Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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?

0
Strabaneboy
Asked:
Strabaneboy
1 Solution
 
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
 
StrabaneboyAuthor Commented:
The subs do, in fact, all belong to the same class and are being called w/o any params.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware 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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now