Using a variable to run a sub or function

This seems so simple....sorry if it's silly.  We'll blame it on the last day before a holiday.  

Given that I have a variable called strSubNm and that the value stored in that variable is the name of one of my subs, how can I use the value stored in that variable to run a subroutine?

StrabaneboyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
Ok, then should be something like this:

Dim mysub as String = "testsub"
Dim method as MethodInfo = myClass.GetType().GetMethod(mysub)
method.Invoke(myClass, Nothing)

where myClass is an instance of your class
0
 
Ashish PatelCommented:
You cannot do that. Search out for some work around for it.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Indeed it is possible, with reflection. I assume the posible subroutines all belong to the same class and all have the same parameter list. Could you specify?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
StrabaneboyAuthor Commented:
The subs do, in fact, all belong to the same class and are being called w/o any params.
0
 
VBRocksCommented:
You can also do it like this:

    Dim strSubNm As String = "MySub"
    CallByName(Me, strSubNm, CallType.Method)


    Public Sub MySub()
        MsgBox("Test")
    End Sub
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*** The "old school" CallByName() method (it's carried over from VB6) proposed by VBRocks requires that the method be PUBLIC...it doesn't work with Private members.
0
 
StrabaneboyAuthor Commented:
This rocks!  Thanks so much.  

0
All Courses

From novice to tech pro — start learning today.