Results 1 to 8 of 8

Thread: Calling Procedures From Strings

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Ireland
    Posts
    15

    Post

    If I read in a string from a database or any other file, and the string is the name of a function in a dll. How do I get VB to call the procedure that is the name of the string that I have read from the file.
    VB does not seem to be able to call procedures from variable names, is there some way I can make the VB interpreter interpret a variable name as the name of a procedure that it should be calling?
    'I am using VB6.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    No, not yet. It looks like VB is going in that direction. The new CallByName function allows you to use strings for Property or Method names, but not Procedure Names. I believe the best you can do now is something like:
    Code:
    Select Case MyImputString
        Case "abc"
            Call abc
        Case "xyz"
            Call xyz
        Case Else
            MsgBox "Unexpected String"
    End Select
    ------------------
    Marty


    [This message has been edited by MartinLiss (edited 01-11-2000).]

  3. #3
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Post

    Check this out Marty:

    I was looking at the documentation (all 2 paragraphs of it) and I see that the argument requires an OBJECT as it's first parameter.

    Now, in order to reference the current form as an object, we have to declare the form as an object. Once we do that, then we are golden:

    Just add a command button to a new form

    Code:
    Private Sub Command1_Click()
    
        
        Dim f1 As Form1    
        Dim procName As String
    
    
        Set f1 = New Form1
        procName = "Hello"
        
        CallByName f1, procName, VbMethod
        
    End Sub
    
    Public Sub Hello()
        MsgBox "Hi!"
    End Sub
    That's it! If you want to call functions in a class module in a COM (ActiveX) DLL, you just need to declare it as an object (as you normally would have to) and pass the appropriate info to CallByName

    Tom

    [This message has been edited by Clunietp (edited 01-11-2000).]

  4. #4
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844

    Post

    Even better! Great catch Marty

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2000
    Location
    Ireland
    Posts
    15

    Post

    The problem with that is that the first argument has to be a valid object type. It would be better if the object type could be a string, which is also read from the database and that string could be evaluated as a valid object type of the same name as the string.

    eg
    dim my_object, my_function as string
    my_object = "an_object" 'a valid user defined type
    my_function = "a_function" 'a valid function on this type of object

    callbyname(my_object, my_function, vbmethod)
    .......
    my_object cannot be interpreted properly as it is a string and my_function is not a valid function on string objects.
    VB does not recognise my_object to be of type an_object like I would want.
    Leaving my_object as a dim doesn't work either.
    Anybody writing VB7 might want to consider putting that kind of functionality in as it would be soo handy.

    Using call by name still gives the same problem only in a different form.

    You would still need a case statement to decide on what type of object the method is to be called as there are many methods with many functions.

    CallByName would seem to be the best that VB has to offer, It would be nice if it were better though.

    Thanks for the feedback

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    Well since forms are objects, even this works!
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        
        Dim procName As String
    
        procName = "Hello"
        
        CallByName Form1, procName, VbMethod
        
    End Sub
    
    Public Sub Hello()
        MsgBox "Hi!"
    End Sub
    ------------------
    Marty

  7. #7
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post

    And while a variant will work, it is signifcantly slower than any other data type...

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Post

    It probably doesn't matter in this case, but did you know that when you dimension variables like you did with dim my_object, my_function as string, that while my_function becomes a String, my_object becomes a Variant?

    ------------------
    Marty

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