Class passing as argument

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.

how can I do that ?

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

more here : https://documentation.xojo.com/getting_started/object-oriented_programming/interfaces.html

2 Likes

I suspect you may be able to use introspection to do that with methodinfo.invoke
https://documentation.xojo.com/api/language/methodinfo.html#methodinfo-invoke
https://documentation.xojo.com/api/language/me.htmlthodInfo

“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

Example project:

1 Like

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.

Xojo is not late binding so I don’t understand how interfaces can help at runtime?

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.


thanks all,

my classes have all the same ancestor, so far so good …

and now, if I pass any of my classes to a window, how do I call a certain Methode of that class[n] where I do not know the name ?

Yes, it is. The actual method to call is resolved at runtime, not compile time.

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.

https://documentation.xojo.com/api/language/is.htmlA

it looks like
base class a
— sub class b
— sub class c

so class b and c have set Super to class a

sorry for misleading you, I wanted to say all classes have the same interface

1 Like

then the parameter type of the method is the interface name and not the class name.
var a as new class1
var b as new class2

could call
MethodXY a
MethodXY b

if the method argument is x As Interface1
inside of the MethodXY you can access all of your interface methods.

its also useful for arrays or propertys
but if you pass a object only as interface you can not cast it back to the whole object.

I have it working now,

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,

thanks again for your help !!!

1 Like

Don’t do this. Using IsA and casting is just reinventing, poorly, something Xojo already has built-in (i.e. method overriding).

Yup, this should be handled within the class or classes. See factory pattern or similar.