• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

Function as parameter of another function



Hi,


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

Y(functionX,...)


Thanks
0
TungVan
Asked:
TungVan
2 Solutions
 
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
0
 
mladenoviczCommented:
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)

0
 
Mike TomlinsonMiddle 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.

~IM
0
 
EDDYKTCommented:
You can see recent post

http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/Visual_Basic/Q_21378696.html

may be use callbyname and pass function name as string
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now