I'm using VBScript and I am wondering if is there any way to call a function name stored in string variable?
Here is my attempt?
a = "b"
sub b()
msgbox "c"
end sub
a()
But it always result in an error
Type mismatch 'a'
The correct answer is: Use GetRef() as in:
Function F(p)
F = p + p
End Function
Dim FP : Set FP = GetRef("F")
WScript.Echo FP("a")
WScript.Echo FP(123)
Output:
aa
246
Seems like e.g. this should work:
Dim a
a = "Call b()"
Eval(a)
Sub b
' Do stuff
End Sub
GetRef is the right (and secure!) way to do it. Eval is not secure, uses global scope, is slow and you cannot easily pass arguments in your function. And a function pointer created by GetRef can be passed around as an object, which is a huge pro.Dim x
Sub b
print "xxx"' Do stuff
End Sub
x = "call b()"
Execute(eval("x"))