One good method is to make a variable that is a delegate--a reference to the function. Then set it to the function that you want to invoke. Here's an example:
Here are the three functions. I added a parameter to show how to handle that case in the delegate.
Private Function A(ByVal x As Integer) As Integer
Return x + 1
End Function
Private Function B(ByVal x As Integer) As Integer
Return x * 2
End Function
Private Function C(ByVal x As Integer) As Integer
Return x * x
End Function
Here's the delegate definition. Variables of this type can be assigned to any function that takes an integer parameter and returns an integer result.
Private Delegate Function ABCFunction(ByVal x As Integer) As Integer
Here's the variable declaration. The variable func can refer to a function.
Dim func As ABCFunction
Finally, here's some code that uses the variable. It assigns it to point to each of the three functions and calls each.
func = AddressOf A
Debug.WriteLine("A: " & func(10))
func = AddressOf B
Debug.WriteLine("B: " & func(10))
func = AddressOf C
Debug.WriteLine("C: " & func(10))
Here are the results:
A: 11
B: 20
C: 100
I hope that helps!
Rod
Rod Stephens, Visual Basic MVP
[email protected]
*** New Book ***
"Visual Basic 2008 Programmer's Reference"
Sign up for the free
VB Helper Newsletters at
http://www.vb-helper.com/newsletter.html