Passing ParamArray to another sub/function
Did any of you tried to pass a ParamArray value to another function ?
It did not work, right ?
Here's what I mean:
VB Code:
Option Explicit
Private Sub Form_Load()
Test1 1, 2, 3, 4, 5, 6
End Sub
Private Sub Test1(ParamArray Values() As Variant)
[COLOR=Red]Test2 Values[/COLOR] ' Gives error here
End Sub
Private Sub Test2(Vals As Variant)
Debug.Print UBound(Vals)
End Sub
Yes, I discovered the fix also, but I don't understand why it works:
VB Code:
Option Explicit
Private Sub Form_Load()
Test1 1, 2, 3, 4, 5, 6
End Sub
Private Sub Test1(ParamArray Values() As Variant)
Test2 Array(Values)(0) ' PROBLEM FIXED
End Sub
Private Sub Test2(Vals As Variant)
Debug.Print UBound(Vals)
End Sub
Theoretically, doing this "Array(some_value)(0)" is the same as just some_value alone... why does that work I have no clue...
Re: Passing ParamArray to another sub/function
well it seems to me the answer is that Array(some_value)(0) and some_value are not the same thing - a ParamArray is not composed of references but actual values. When you do Array(some_value)(0) you are creating a copy of the ParamArray, thereby avoiding the 'can't pass paramarray byref' problem - e.g.
VB Code:
Private Sub Form_Load()
Test1 1, 2, 3, 4, 5, 6
End Sub
Private Sub Test1(ParamArray Values() As Variant)
Test2 Array(Values)(0) ' PROBLEM FIXED
MsgBox Values(0) 'You get 1 - not 10
End Sub
Private Sub Test2(ByRef Vals As Variant)
Vals(0) = 10
End Sub
As to why you can't pass paramarrays byref in the first place - i don't know.
Re: Passing ParamArray to another sub/function
Thanks,
It did not cross my mind to try to pass the ParamArray by value, now I see that I don't need Array()(0), I just have to make the prototype with ByVal...
Re: Passing ParamArray to another sub/function
whoops in my example it should have been ByRef not ByVal (otherwise the point i was making doesn;t make sense):
VB Code:
Private Sub Test2([B]ByRef[/B] Vals As Variant)
Vals(0) = 10
End Sub
But yes, the solution is to pass it by value or assign the ParamArray to a variant before passing it ByRef.
Re: Passing ParamArray to another sub/function
A bit of an offtopic, but: I can't figure out any real use for ParamArray. It might be easier to declare than several parameters, but besides that... there is always the maximum parameters and you always know how many parameters you give to a procedure. So: is there any real use besides programming comfort that I haven't noticed?
Re: Passing ParamArray to another sub/function
Quote:
Originally Posted by Merri
A bit of an offtopic, but: I can't figure out any real use for ParamArray.
Well, in my case, I don't know how else to do it
I'm making an ActiveX control that encapsulates the ListView, I want to make a combination between TreeView and ListView (But I'm not actually using any TreeView objects)
I have to make a property that will accept as many values as are in the colums that where you add it
This is my function prototype:
VB Code:
Public Function InsertItem(ParamArray Values() As Variant) As String
Then I can use it like this:
VB Code:
ucListViewTree1.DataList.InsertItem "c", "04", "aaa"
ucListViewTree1.DataList.InsertItem "x", "05"