Function as parameter of another function


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


Who is Participating?
mladenoviczConnect With a Mentor Commented:
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)

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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.

You can see recent post

may be use callbyname and pass function name as string
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.