[Gambas-user] Run a function from a string?
Bruce Steers
bsteers4 at gmail.com
Sat Aug 28 16:25:11 CEST 2021
On Sat, 28 Aug 2021 at 14:05, Tobias Boege via User <
user at lists.gambas-basic.org> wrote:
> 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.
>
> Best,
> Tobias
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>
Aaah , that's fantastic Tobias , many thanks.
So now I've made a simple class file called RunFree.class ...
*' Gambas class fileExportStatic Private $Timer As TimerStatic Private
$Parent As ObjectStatic Private $Method As StringStatic Private $ArgArray
As Variant[]Static Public Sub _init() $Timer = New Timer As "Timer"
$Timer.Delay = 0EndStatic Public Sub _call(Parent As Object, Method As
String, Optional ArgArray As Variant[]) $Parent = Parent $Method =
Method $ArgArray = ArgArray $Timer.StartEndStatic Public Sub
Timer_Timer()$Timer.Stop*
*Object.Call($Parent, $Method, $ArgArray)End*
Now during my Event handler i can simply use
RunFree(Me, "MyFunctionName", ["Plus", "Args"])
That way the function (sorry Method, thanks for the correlation correction
:) ) is run asynchronous of the event handler that is freed to move on :)
The answer to my current set of prayers :)
Awesome stuff , that's going to open up some options for me now :)
Thanks again Tobias :)
Respects
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20210828/07a25dd4/attachment.htm>
More information about the User
mailing list