Results 1 to 6 of 6

Thread: Call a Sub with Sub Name as String [Not Solved]

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    15

    Call a Sub with Sub Name as String [Not Solved]

    Is it possible to call a sub with a string.

    vb Code:
    1. Dim subname as string
    2. subname = textbox1.text
    3. (Code for calling sub, subname)
    Any help?
    All help is appreciated
    Last edited by CrisIsRatedX; Sep 2nd, 2011 at 07:41 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call a Sub with Sub Name as String [Not Solved]

    It is possible but it's a rare thing to have to do, and having the user type it in is something that you would pretty much never do. Can you explain, fully and clearly, exactly what you're trying to achieve and we can then advise the best way to achieve it, whether that be using a String to store a method name or something else?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    15

    Re: Call a Sub with Sub Name as String [Not Solved]

    It would be using a string as the method name. I was just using a textbox to show that it should be able to be any string. Just have the name of the method/sub.

    vb Code:
    1. dim subname as string
    2. subname = "sub"

    Then have it use that string subname to run the method/sub.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Call a Sub with Sub Name as String [Not Solved]

    I'm with JMC. While it's possible it is slow, complicated, and error-prone. If you would explain what you are doing instead of how you are trying to do it, we can probably suggest a faster, easier, more reliable method. There's many ways to call a method based on what a user has done, and encoding the information as strings is usually the last resort.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    15

    Re: Call a Sub with Sub Name as String [Not Solved]

    I am making a function that is in a different vb class library. The user needs to put in the path of a directory and the name of the method the user will be using in the code.

    vb Code:
    1. Public Shared Function getList(ByVal path As String, ByVal method As String)
    2. Dim count As Integer = System.IO.Directory.getFiles(path).Length
    3. Dim dArr() As String = System.IO.Directory.getFiles(path)
    4. Dim i As Integer
    5. For i = 0 To count - 1
    6. (Call Method Here)
    7. Next
    8. Return 1
    9. End Function

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Call a Sub with Sub Name as String [Not Solved]

    Why would a user of your application know the name of a method in the original source code? That is diabolical. Furthermore, you still haven't provided what I would consider a "full and clear description". As Sitten Spynne said, you are still explaining how you're trying to do it and not what you're trying to do.

    If you are determined to go down this path then I will say that you should investigate Reflection. I'm not willing to go into more detail if you aren't though, because I still have serious doubts that it's not a waste of time because it's not the most appropriate option. If you are unable or unwilling to provide a proper description of the purpose of your application (NOT its implementation) then I'm afraid that I, for one, am unable or unwilling to help further.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width