[Gambas-user] Run a function from a string?

Tobias Boege tobs at taboege.de
Sat Aug 28 14:58:54 CEST 2021

On Sat, 28 Aug 2021, Bruce Steers wrote:
> Is it ,, could it possibly be possible to run a function from a String?
> It would open up all sorts of possibilities.
> What i mean is for example maybe i want to make a timer to trigger a
> function so i want to supply the function name but not run the function at
> that time.
> Eg.
> Public Sub DeferCommand_Click()
>   sFunctionName="RunNow"
>   sFunctionArgs="a message"
>   tFunctionTimer.Start()
> End
> Public Sub FunctionTimer_Timer()
>   Last.Stop
> '' This bit is where we hit a wall because i don't think anything like it
> is possible, but would be sooo useful if it was.
>   FMain.Functions[sFunctionName].Call(sFunctionArgs)
> End
> Public Sub RunNow(sMessage As String)
>   Print "Running Now\n" & sMessage
> End
> -------------
> I should imaging the 3 dots ... could be utilized to transmit any number of
> arguments
> I of course have no real clue where to even begin to implement anything
> like this.
> That would be our Benoits bag of amazing insights :)
> (<-- stands back and waits for the "nope, not ever!" :'( ;) )
> Any kind of way to implement something like that would be incredibly useful.
> I hope I have been clear in my wish here.
> kinda like bash 'eval' can turn a string into a function and run it.

Eval() exists in Gambas as well, but in this case I would use Object.Call().
Your FMain.Functions[sFunctionName].Call(aFunctionArgs) becomes

  Object.Call(FMain, sFunctionName, aFunctionArgs) ' arguments are an Array

Remember that Gambas has almost no "functions". It has objects which have
methods. It's better to think about it that way. It also implies that you
need to have an object available to call a method on. An automatically
created one like FMain suffices.


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

More information about the User mailing list