Results 1 to 6 of 6

Thread: Calling a function based on the contents of a string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    23

    Calling a function based on the contents of a string

    Hi all, is it possible to call a function based on the name of a string?

    What I want to do is treat the contents of "TextVariable" as a function name and then call it...

    i.e. something similar to this:

    VB Code:
    1. Sub Command1_Click()
    2.     Msgbox "Button pressed"
    3. End Sub
    4.  
    5. Sub Command2_Click()
    6.     Dim TextVariable as String
    7.  
    8.     TextVariable = GetFunctionNameFromDatabase()
    9.     'i.e. TextVariable = "Command1_Click()"
    10.  
    11.     Call ContentsOf(TextVariable)
    12. End Sub

    cheers
    - Mike.

  2. #2
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046
    probably the closest you will get to doing this will be to put your function code in a Class Property or Method and use the CallByName function to invoke it (I think that would work anyway) ...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    23
    okies, cheers :-)

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    Another thing you could do would be to make a function that took the string as an argument, and used a Select statement to choose which function to call:

    Select funcName
    Case "Blue"
    Blue()
    Case "Red"
    Red()
    .
    .
    .
    etc.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Posts
    23
    yeah that'd work,
    I'm missing the obvious answers and going straight into the more complex ones as usual, ta :-)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    That's when it's time to take a walk.

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