Results 1 to 4 of 4

Thread: Can the address of a VB Function be used to execute the function in some way?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2015
    Posts
    38

    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 ***

  2. #2

    Thread Starter
    Member
    Join Date
    Dec 2015
    Posts
    38

    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.
    Last edited by VBExplorer12; Jun 9th, 2021 at 02:11 PM.

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    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>

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2015
    Posts
    38

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width