+ Reply to Thread
Results 1 to 12 of 12

Call Function by Name

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2005
    Posts
    9

    Call Function by Name

    Just wonder how to call the function if I have it's name in the variable String
    for example something like that:
    sub runFun()
         dim name as string
         name = "test"
         result=callByName(?,name,?,?)
    end sub
    
    Function test() as Boolean
          test=True
    End Function

    maybe there are some other ways?
    Thank you for your help,
    arbus
    Last edited by arbus; 08-30-2007 at 09:56 AM.

  2. #2
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667
    I completed your example to illustrate it better:
    Sub runFun()
    Dim name As Boolean
    name = test
    result = CallByName("Charlie", name, "Frank")
    End Sub
    
    Function test() As Boolean
    test = True
    'test = False
    End Function
    
    Function CallByName(a As String, name As Boolean, b As String)
    If name = True Then
        MsgBox a
    Else
        MsgBox b
    End If
    CallByName = name
    End Function
    Best regards,

    Ray

  3. #3
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    Quote Originally Posted by raypayette
    I completed your example to illustrate it better:
    hi ray
    I am not sure that I understood you
    I used CallByName fucntion because there is such a fuction which executes a method of an object
    So I thought maybe it can be used for calling a function if u have its name saved in the string variable

    in my example I know that there is a function named "test" and I wanted to call that function
    Imagine u have a list of similar finctions: test1, test2, test3 etc
    you want to call them in the loop
    so inside the loop u will assign a string variable name="test"+cstr(i) and afterfards u want to call the function with name="test"+cstr(i)
    Last edited by arbus; 08-30-2007 at 09:59 AM.

  4. #4
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667
    I tried to work out the glitches. For example result = "test" will not use the function test, rather it defines it as the string test; I correct3ed it to use the Boolean function "test". Also you redefined name as a text when it was a Boolean value; I corrected it.

  5. #5
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    any ideas?

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try this. In C1 enter the value xxx.

    Then run
    Sub runFun()
         Dim name As String
         name = "value"
         result = CallByName(Range("C1"), name, VbGet)
         MsgBox result
         name = "Address"
         MsgBox CallByName(Range("C1"), name, VbGet)
         
    End Sub
    The first result will be xxx which is the value in C1. The second answer will be $C$1 which is the address of C1.

    So unless your object has a method of test, then you cannot use this to call it.

    Does that help to make things clearer???


    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1