[Gambas-user] About call sub from string
ocoquet at 3d-phenomen.fr
Thu Jan 7 14:07:11 CET 2021
this is the answer that I wanted :)
regards and many thanks for that.
Le 07/01/2021 à 13:54, Tobias Boege a écrit :
> On Thu, 07 Jan 2021, Olivier Coquet wrote:
>> Hi all,
>> Is somebody to know if it's possible to call a sub from a stringvar which
>> contain the name of the sub ?
>> I've tryed EVAL but don't work :(
>> exemple of what I want to do:
>> public sub main()
>> Dim mavar as string = "masub()"
>> eval (mavar,"")
>> public sub masub()
> Except the built-in functions, Gambas has no free-standing subroutines,
> it only has methods. To call them you need some kind of object which
> provides a context to the method. Eval does not do that for you.
> [ When you call masub() in Gambas code, that code is always in a class
> and will always be executed in the context of some object. In that call,
> the method name is lexically resolved with respect to the current class
> and implicitly called in the context of the current object, aka Me. ]
> Once you have the method name mavar and an object x, you can use
> Object.Call(x, mavar) ' optional Array of arguments after mavar
> Object.Call(Me, mavar) ' if you don't have a specific x, use x = Me
More information about the User