Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a variable to run a sub or function

Posted on 2007-11-21
7
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview

610 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