calling a sub/function whose name is the value of a variable?

  • Thread starter Thread starter H
  • Start date Start date
H

H

dim subName = "GetTable" & strCtr & "()"

I want to now call the GetTable1() sub or GetTable2() sub, depending
on
the value of strCtr.

How can I call the function whose name is in the variable subName?


thanks...
 
If subName = "GetTable1()" then
Call GetTable1()
elseif SubName = "GetTable2()" then
Call GetTable2()
Endif

But why bother with SubName? Just use strCtr instead of it.

Bill
 
thanks for the reply, however, I was in search of a way to call the
actual value of the variable

for example...

public sub DynamicCaller( ByVal strCtr as Integer)
Dim subName = "GetTable" & strCtr & "()"
'strCtr can be between 1 and 9

'here, need to call the function name of whatever is in subName
magicMethod(subName) 'doesn't necessarily have to be a method
name, should be anything that literally runs the value of the variable
' this magic method will either call GetTable1() or GetTable2() or
etc....

End Sub
 
H,

If you can live with its limitations, CallByName is the easiest way to do
this in VB.

Kerry Moorman
 
Hi H,

Why making it yourself difficult.
This can be typed in 10 minutes.

GetTable1
GetTable2
GetTable3
....
GetTable9

Cor
 
dim subName = "GetTable" & strCtr & "()"

I want to now call the GetTable1() sub or GetTable2() sub, depending
on
the value of strCtr.

How can I call the function whose name is in the variable subName?

thanks...

Here's an example of doing a late bound call to Console.WriteLine.

Public Sub Example()
Dim methodName As String = "WriteLine"
Dim methodArgs() As Type = { }
Dim method As MethodInfo = GetType(Console).GetMethod(methodName,
methodArgs)
method.Invoke(Nothing, Nothing)
End Sub
 
Back
Top