Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass a function to a function

Status
Not open for further replies.

bojisheng

Programmer
May 16, 2002
15
0
0
US
I am creating a library of standard functions. I would like to provide the ablity to allow a "callback" function. To set this up, I would like to allow the user to pass the function to be called as a parameter. Like passing the address to a function in C. Does anyone know how/if this can be done in VB? And, if so, how I could invoke the passed function (like &xxx(a,b) in C).

Thanks in advance.
 
In a limited sense, yes. Check out VB's CallByName function. Or you can hijack the CallWindowProc API call. Another option involves some hacky tricks involving adding the library functions to a class module and then invoking them by name through the Microsoft Scripting Runtime library
 
Take a look at VB.NET's Delegate statement:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vblr7/html/vastmDelegate.htm

You declare a function prototype as being a Delegate function:
Code:
Delegate Function DelegateFunc(ByVal X As String, _
                               ByVal Y As String) As String
The "DelegateFunc" then becomes a type (similar to a class), which you can then pass to other functions:
Code:
Sub DoSomething(MyDelegateFunc As DelegateFunc)
  Dim R As String  

  ' Invoke the delegate
  R = MyDelegateFunc.Invoke("One ", "Two")
  Console.WriteLine(R)
End Sub
Of course, the delegate function doesn't actually do anything. It's just a placeholder for a function that actually does the work:
Code:
Function ConcatStrings(ByVal A As String, _
                       ByVal B As String) As String
  ConcatString = A & B
End Function
You then pass the delegate function to the sub using the AddressOf operator:
Code:
  DoSomething(AddressOf Me.ConcatStrings)
[code]
Seems like a lot of work, but what it allows you to do is define a number of different implementations of the delegate that all have the same call signature (takes two strings, returns another string), and you can pass the one you want to call to your DoSomething sub.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Never mind my post. Sorry -- I thought I was in the VB.NET forum.

I won't red-flag it, however. Maybe it'll be of some use to someone who is thinking of making the move to .NET.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Thanks for the Tip. I used the following as a test and it worked! (execute test2)
Function test(i As Integer)
check = MsgBox(i, vbCritical, "i am here")

test = i * 2

End Function

Sub test1()
test 5

End Sub

Sub test2()

j = CallByName(ThisWorkbook, "test", VbMethod, 10)

x = MsgBox(j, vbInformation, "And Back")

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top