I have several classes with methods having the same names in each class.
what I want to do is passing the class as an argument to a common window where I want to call those methods.
class1
method1
method2
class2
method1
method2
call a window (class2)
in the window I do not know if it is class1 or class2, all I need is a way to call method1 or method2 of passed class.
you need what is called a classinterface in xojo.
add the methods common to your classes in that class interface
make the classes belong to the classinterface
“I want to invoke a different version of a method depending on the class of the object I have a reference to without knowing at the point of invokation exactly what kind of object it is or which version of the method ought to be called.”
This is a typical use case for Xojo’s class-based dynamic polymorphism, AKA method overriding. In this sort of construct you have several subclasses of a single ancestor class. The ancestor implements set of methods that the subclasses then override with their own versions.
Class CommonAncestor
Sub Method1()
Sub Method2()
End Class
Class Class1
Inherits CommonAncestor
Sub Method1() ' overrides CommonAncestor.Method1
Sub Method2() ' overrides CommonAncestor.Method2
End Class
Class Class2
Inherits CommonAncestor
Sub Method1() ' overrides CommonAncestor.Method1
Sub Method2() ' overrides CommonAncestor.Method1
End Class
You can now create references to objects of type CommonAncestor without knowing ahead of time which subclass will be used:
Dim myinstance As CommonAncestor
If SomeCondition = True Then
myinstance = New Class1()
Else
myinstance = New Class2()
End If
When a method of CommonAncestor is called it’s automatically sent to the correct subclass’s overridden version:
Sub DoSomething(myinstance As CommonAncestor)
myinstance.Method1() ' call a method, which might be overridden
End Sub
if you gave both classes the same interface you would pass the object As InterfaceName, then you see only methods of this interface.
a interface make functionality consistent.
the other suggestion is used if customer A use class A and customer B use class B if one executable is used for both.
the task was to give the window a object XY and there you will call a method ABC.
as example 2 different classes but in the method you will access the object via dot methodname.
if the classes all have the same ancestor then no need for a class interface.
you use the class ancestor a parameter type for the window
then inside the window, use the Isa keyword and a casting
to detect the actual class child you’re passing.
in the window I have a property as the interface and pass a class, load it into the interface property and now I can call all the methods from the passed property, super,