Calling a function who's name is trapped in a variable

  • Thread starter Thread starter GV
  • Start date Start date
G

GV

Hi,

I am new to VB. I want to be able to call different
function from within another function.

e.g Private Sub FunctionCaller(FunctionName As String)
Call FunctionName(Parameters)
End Sub

Now every time I excute the FunctionCaller it always
return a error message "Compile Error , Execpt Sub,
Function or property"???

I dont know if either I am missing something or I am
totally doing it wrong.

Anyone with any sugessations or any other way of achieving
it?

TIA
GV
 
GV,
You can use the CallByName function. Or I prefer to use a Delegate:

Something like:

Delegate Sub FunctionName(ByVal a As Integer, ByVal b As String)

Private Sub FunctionCaller(ByVal theFunction As FunctionName)
Dim anInteger As Integer
Dim aString As String

theFunction.Invoke(anInteger, aString)
' or
theFunction(anInteger, aString)

End Function

Private Sub SomeFunction1(ByVal a As Integer, ByVal b As String)
End Sub

Private Sub SomeFunction2(ByVal a As Integer, ByVal b As String)
End Sub

Then when you call the above you use AddressOf

FunctionCaller(AddressOf SomeFunction1)
FunctionCaller(AddressOf SomeFunction2)

Hope this helps
Jay
 
GV said:
I am new to VB. I want to be able to call different
function from within another function.

e.g Private Sub FunctionCaller(FunctionName As String)
Call FunctionName(Parameters)
End Sub

Now every time I excute the FunctionCaller it always
return a error message "Compile Error , Execpt Sub,
Function or property"???

I dont know if either I am missing something or I am
totally doing it wrong.

Have a look at 'CallByName'.
 
Back
Top