Results 1 to 6 of 6

Thread: [RESOLVED] Using CallByName with variable number of arguments

  1. #1

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Resolved [RESOLVED] Using CallByName with variable number of arguments

    I want to use CallByName to call different functions on an object.
    The functions can all have a different number of arguments.
    CallByName is defined as:
    Function CallByName(Object As Object, ProcName As String, CallType As VbCallType, Args() As Variant)

    But when i pass a variant array, an error is generated (Argument not optional)
    It works when I pass the arguments one by one, but i need it to work with a variable number of parameters.

    The following is a sample to reproduce the problem.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim params() As Variant
    3. Dim Result As Variant
    4.     ReDim params(1)
    5.     params(0) = "Test"
    6.     params(1) = "test"
    7.     Result = CallByName(Me, "MyFunction", VbMethod, "Test", "Test") '<-- This will work
    8.     Result = CallByName(Me, "MyFunction", VbMethod, params) '<-- The produces an error
    9. End Sub
    10.  
    11.  
    12. Public Function MyFunction(ByVal a As String, ByVal b As String) As String
    13.     MyFunction = a & b
    14. End Function
    Frans

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using CallByName with variable number of arguments

    Unfortunatly it doesn't work that way. The Args() array is actually a ParamArray. You could of course do something like the following with a long select case statement.
    VB Code:
    1. Public Function CallFunction(sFuncName As String, vParams() As Variant)
    2.     If IsArray(vParams) Then
    3.         Select Case UBound(vParams)
    4.             Case 0
    5.                 CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0))
    6.             Case 1
    7.                 CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0), vParams(1))
    8.             Case 2
    9.                 CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0), vParams(1), vParams(2))
    10.             'and so on
    11.         End Select
    12.     Else
    13.         CallFunction = CallByName(Me, sFuncName, VbMethod, vParams)
    14.     End If
    15. End Function

  3. #3

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Using CallByName with variable number of arguments

    Yes, that is also the solution I came up with, but I don't really like it. I was hoping for a more generic solution.

    Thanks anyway
    Frans

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Using CallByName with variable number of arguments

    You can do what you want by using CallByNameEx function, in a module..
    VB Code:
    1. Public Function CallByNameEx(Obj As Object, _
    2.     ProcName As String, CallType As VbCallType, _
    3.     Optional vArgsArray As Variant)
    4.     Dim oTLI As Object
    5.     Dim ProcID As Long
    6.     Dim numArgs As Long
    7.     Dim i As Long
    8.     Dim v()
    9.    
    10.     On Error GoTo Handler
    11.    
    12.     Set oTLI = CreateObject("TLI.TLIApplication")
    13.     ProcID = oTLI.InvokeID(Obj, ProcName)
    14.    
    15.     If IsMissing(vArgsArray) Then
    16.         CallByNameEx = oTLI.InvokeHook( _
    17.             Obj, ProcID, CallType)
    18.     End If
    19.    
    20.     If IsArray(vArgsArray) Then
    21.         numArgs = UBound(vArgsArray)
    22.         ReDim v(numArgs)
    23.         For i = 0 To numArgs
    24.             v(i) = vArgsArray(numArgs - i)
    25.         Next i
    26.         CallByNameEx = oTLI.InvokeHookArray( _
    27.             Obj, ProcID, CallType, v)
    28.     End If
    29. Exit Function
    30.  
    31. Handler:
    32.     Debug.Print Err.Number, Err.Description
    33. End Function
    Then, use the same code you were using, but use CallByNameEx when you want to use an array..
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim params() As Variant
    3. Dim Result As Variant
    4.     ReDim params(1)
    5.     params(0) = "Test"
    6.     params(1) = "test"
    7.     Result = CallByName(Me, "MyFunction", VbMethod, "Test", "Test") '<-- This will work
    8.     'With arrays, use CallByNameEx..
    9.     Result = CallByNameEx(Me, "MyFunction", VbMethod, params)       '<-- This will work also
    10. End Sub
    11.  
    12. Public Function MyFunction(ByVal a As String, ByVal b As String) As String
    13.     MyFunction = a & b
    14. End Function

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Using CallByName with variable number of arguments

    Cool jcis, I've forgot about the TypeLib Information Library

  6. #6

    Thread Starter
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Using CallByName with variable number of arguments

    Great jcis, it works like a charm.
    Frans

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