Solved

Using a variable to run a sub or function

Posted on 2007-11-21
7
157 Views
Last Modified: 2013-11-07
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?

0
Comment
Question by:Strabaneboy
7 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
Comment Utility
You cannot do that. Search out for some work around for it.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
 

Author Comment

by:Strabaneboy
Comment Utility
The subs do, in fact, all belong to the same class and are being called w/o any params.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
Comment Utility
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
 
LVL 27

Expert Comment

by:VBRocks
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
*** 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
 

Author Closing Comment

by:Strabaneboy
Comment Utility
This rocks!  Thanks so much.  

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now