How to use a String Variable as Function Name?
LausX
does anybody know how to use a string as a function name? I want to avoid a lot if switch or if-else chains and therefore directly "build" the name of a function according to the DoPrompt-String.
For example, if you type the following in the command line everything works well:
Function Test_Function(a)
Variable a
return a
End
// type the following also in the command line:
String ab = "Test_Function"
print $ab(2)
Variable a
return a
End
// type the following also in the command line:
String ab = "Test_Function"
print $ab(2)
If I use the same concept inside a function Igor tells me "can't use $ inside a function in this way". Is there any possibility for this?
Thanks a lot
A better way is to use FUNCREFs. For example:
Print "FProto called"
End
Function F1()
Print "This is F1"
End
Function F2()
Print "This is F2"
End
Function Test(val)
Variable val // 1 or 2
String functionName
sprintf functionName, "F%d", val
FUNCREF FProto f = $functionName
f()
End
November 3, 2013 at 10:01 am - Permalink
I like the idea to use FUNCREF's.
November 15, 2013 at 02:27 am - Permalink