Solved

Using a variable to run a sub or function

Posted on 2007-11-21
7
160 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
ID: 20329296
You cannot do that. Search out for some work around for it.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20329324
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
ID: 20329345
The subs do, in fact, all belong to the same class and are being called w/o any params.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 20329414
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
ID: 20329470
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
ID: 20329495
*** 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
ID: 31410413
This rocks!  Thanks so much.  

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 42
JSON  parse help 7 40
SQL Query Help Top 1 and Distinct? 6 36
Syntax Issue with SSIS module 26 105
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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