Can the address of a VB Function be used to execute the function in some way?
Hi,
Outside of an Events context, can the address of a function obtained with GetRef(), be used in a variable which will later be used to execute the function?
What I'm trying to accomplish is sending the function name as parameter on the command line, and then executing that function inside the script, something like:
WScript test.vbs "FirstFunction"
Code:
'**** Test.vbs
Option Explicit
Function ExecuteFunction( FunctionName )
Dim RetVal
RetVal = RunThis( &Function )
ExecuteFunction = RetVal
End Function
' **** All defined functions ****
Function FirstFunction
...
End Function
Function SecondFunction
...
End Function
'*** End of test.vbs ***
Re: Can the address of a VB Function be used to execute the function in some way?
It looks like the answer might be Eval(). Or get the address with GetRef, then Call the variable with parentheses.
I'll play with those.
Re: Can the address of a VB Function be used to execute the function in some way?
GetRef returns an IDispatch (an Object) with only a default method implemented which can be called with parentheses like this
Code:
Option Explicit
Dim oFunctor
Set oFunctor = GetRef(WScript.Arguments.Item(0))
oFunctor(WScript.Arguments.Item(1))
' **** All defined functions ****
Function FirstFunction(Param)
WScript.echo "Enter FirstFunction w/ Param=" & Param
End Function
Function SecondFunction(Param)
WScript.echo "Enter SecondFunction w/ Param=" & Param
End Function
Then try on the command line something like this
c:> cscript aaa.vbs FirstFunction 123
cheers,
</wqw>
Re: Can the address of a VB Function be used to execute the function in some way?
Thanks wq,
I had a problem using the GetRef, when applied to a function. It returned the value of the function, rather than an address.
At any rate, Eval() is working perfectly.
Thanks for the tips, I'll put those in the pim.