CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2003
    Posts
    3

    Call sub from variable

    I would like to call a sub (or function if necessary) from a variable. Currently I am having difficulties doing this. I get an error. The error is Compile error: Expected Sub, Function, or Property. The code I am using is below:

    Sub Command1_Click()
    SubToCall = "Call" & "Me"
    SubToCall
    End Sub

    Sub CallMe()
    ' Process data here
    End Sub

    Any suggestions on how to make this work would be greatly apprecieated.

    Thanks

  2. #2
    Join Date
    Jan 2003
    Posts
    25
    vbwannabe

    I dont know if what you want to do is possible. What exactly are you trying to do? there is an addressof() function that allows you to get the address of a function. or maybe you can use a case structure for example

    Private Sub Command_Click()
    Select Case VaribleName
    Case FunctionNameOne
    FunctionNameone()
    Case FunctionNameTwo
    FunctionNameTwo()
    Case Else
    Messagebox "No such Function Name"
    End Select
    End sub

    sorry best i can do
    Jerry Woodstock

  3. #3
    Join Date
    Mar 2003
    Posts
    3
    I need to process and evaluate incoming comm port data 1 of 16 different ways. The process varies widely so I will not have any common code. The process will not change during the program execution and is determined at startup by a series of radio buttons. My intent was to run the proper procedure based on radio button input. I would assign a variable to the radio button which would execute the correct procedure on incoming comm port data. I did not want to evaluate a select case for each data input since the process would remain constant throughtout the program execution. I appreciate your input. Thanks for taking the time to respond.

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332
    The CallByName Function will do what you want. You could even append the index of the option button to the name of the function too.

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

  6. #6
    Join Date
    Mar 2003
    Posts
    3
    Thanks for the input. I will give this a try.

  7. #7
    Join Date
    Aug 2002
    Location
    Lucknow
    Posts
    129
    Use CallByName

    eg.

    Code:
    Public Function calval(ByVal x As Integer) As Integer
        calval = x * x
    End Function
    
    Private Sub Form_Load()
        MsgBox CallByName(Form1, "calval", VbMethod, 10)
    End Sub
    Last edited by Cimperiali; May 20th, 2004 at 02:19 AM.

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

Featured