[Gambas-user] About call sub from string

Tobias Boege taboege at gmail.com
Thu Jan 7 13:54:05 CET 2021

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,"")
> endsub
> public sub masub()
> endsub

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


"There's an old saying: Don't change anything... ever!" -- Mr. Monk

More information about the User mailing list