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

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...
 
B

billsahiker

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
 
H

H

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
 
G

Guest

H,

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

Kerry Moorman
 
C

Cor Ligthert [MVP]

Hi H,

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

GetTable1
GetTable2
GetTable3
....
GetTable9

Cor
 
B

Brian Gideon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top