-
May 15th, 2006, 06:26 AM
#1
[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:
Private Sub Command1_Click()
Dim params() As Variant
Dim Result As Variant
ReDim params(1)
params(0) = "Test"
params(1) = "test"
Result = CallByName(Me, "MyFunction", VbMethod, "Test", "Test") '<-- This will work
Result = CallByName(Me, "MyFunction", VbMethod, params) '<-- The produces an error
End Sub
Public Function MyFunction(ByVal a As String, ByVal b As String) As String
MyFunction = a & b
End Function
-
May 15th, 2006, 06:42 AM
#2
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:
Public Function CallFunction(sFuncName As String, vParams() As Variant)
If IsArray(vParams) Then
Select Case UBound(vParams)
Case 0
CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0))
Case 1
CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0), vParams(1))
Case 2
CallFunction = CallByName(Me, sFuncName, VbMethod, vParams(0), vParams(1), vParams(2))
'and so on
End Select
Else
CallFunction = CallByName(Me, sFuncName, VbMethod, vParams)
End If
End Function
-
May 15th, 2006, 06:55 AM
#3
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
-
May 15th, 2006, 08:02 AM
#4
Re: Using CallByName with variable number of arguments
You can do what you want by using CallByNameEx function, in a module..
VB Code:
Public Function CallByNameEx(Obj As Object, _
ProcName As String, CallType As VbCallType, _
Optional vArgsArray As Variant)
Dim oTLI As Object
Dim ProcID As Long
Dim numArgs As Long
Dim i As Long
Dim v()
On Error GoTo Handler
Set oTLI = CreateObject("TLI.TLIApplication")
ProcID = oTLI.InvokeID(Obj, ProcName)
If IsMissing(vArgsArray) Then
CallByNameEx = oTLI.InvokeHook( _
Obj, ProcID, CallType)
End If
If IsArray(vArgsArray) Then
numArgs = UBound(vArgsArray)
ReDim v(numArgs)
For i = 0 To numArgs
v(i) = vArgsArray(numArgs - i)
Next i
CallByNameEx = oTLI.InvokeHookArray( _
Obj, ProcID, CallType, v)
End If
Exit Function
Handler:
Debug.Print Err.Number, Err.Description
End Function
Then, use the same code you were using, but use CallByNameEx when you want to use an array..
VB Code:
Private Sub Command1_Click()
Dim params() As Variant
Dim Result As Variant
ReDim params(1)
params(0) = "Test"
params(1) = "test"
Result = CallByName(Me, "MyFunction", VbMethod, "Test", "Test") '<-- This will work
'With arrays, use CallByNameEx..
Result = CallByNameEx(Me, "MyFunction", VbMethod, params) '<-- This will work also
End Sub
Public Function MyFunction(ByVal a As String, ByVal b As String) As String
MyFunction = a & b
End Function
-
May 15th, 2006, 08:10 AM
#5
Re: Using CallByName with variable number of arguments
Cool jcis, I've forgot about the TypeLib Information Library
-
May 15th, 2006, 09:25 AM
#6
Re: Using CallByName with variable number of arguments
Great jcis, it works like a charm.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|