Function as parameter of another function

Posted on 2005-04-06
Last Modified: 2008-02-01


I would like to know if it is possible to pass a functionX as a parameter of a function called Y


Question by:TungVan
    LVL 65

    Expert Comment

    by:Jim Horn
    Not sure, but this would likely be cleaner....

    Dim sString as string
    sString = FunctionX

    Call Y(sString, ...)

    ... or ...

    Call Y (...)

    Public FunctionY ( ...) as Whatever

    Dim sString as string
    sString = FunctionX

    'rest of function goes here

    end function
    LVL 8

    Accepted Solution

    There are function pointers
    MSDN: A function pointer is a convention that enables you to pass the address of a user-defined function as an argument to another function you've declared for use within your application.

    Storing a Function Pointer in a Variable
    At times, you may need to store a function pointer in an intermediate variable before passing it to the DLL. This is useful if you want to pass function pointers from one Visual Basic function to another. It's required if you are calling a function like RegisterClass, where you need to pass the pointer through an argument to a structure (WndClass), which contains a function pointer as one of its elements.

    To assign a function pointer to an element in a structure, you write a wrapper function. For example, the following code creates the wrapper function FnPtrToLong, which can be used to put a function pointer in any structure:

    Function FnPtrToLong (ByVal lngFnPtr As Long) As Long
       FnPtrToLong = lngFnPtr
    End Function

    To use the function, you first declare the type, then call FnPtrToLong. You pass AddressOf plus your callback function name for the second argument.

    Dim mt as MyType
    mt.MyPtr = FnPtrToLong(AddressOf MyCallBackFunction)

    LVL 8

    Expert Comment

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Here is a key sentence from the article (near the bottom) referenced by mladenovicz:

        "Basic to Basic" function pointers are not supported. Pointers to Visual Basic functions cannot be passed within Visual Basic itself. Currently, only pointers from Visual Basic to a DLL function are supported.

    So the answer to your question is NO.  You cannot pass the address of one function to another function within VB itself.

    The AddressOf function is only useful when dealing with APIs that require a callback.

    LVL 26

    Expert Comment

    You can see recent post

    may be use callbyname and pass function name as string

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now