Results 1 to 6 of 6

Thread: Passing ParamArray to another sub/function

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Test1 1, 2, 3, 4, 5, 6
    5. End Sub
    6.  
    7. Private Sub Test1(ParamArray Values() As Variant)
    8.     [COLOR=Red]Test2 Values[/COLOR]  ' Gives error here
    9. End Sub
    10.  
    11. Private Sub Test2(Vals As Variant)
    12.     Debug.Print UBound(Vals)
    13. End Sub
    Yes, I discovered the fix also, but I don't understand why it works:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Test1 1, 2, 3, 4, 5, 6
    5. End Sub
    6.  
    7. Private Sub Test1(ParamArray Values() As Variant)
    8.     Test2 Array(Values)(0)  ' PROBLEM FIXED
    9. End Sub
    10.  
    11. Private Sub Test2(Vals As Variant)
    12.     Debug.Print UBound(Vals)
    13. 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...

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Sub Form_Load()
    2.     Test1 1, 2, 3, 4, 5, 6
    3. End Sub
    4.  
    5. Private Sub Test1(ParamArray Values() As Variant)
    6.     Test2 Array(Values)(0)  ' PROBLEM FIXED
    7.     MsgBox Values(0) 'You get 1 - not 10
    8. End Sub
    9.  
    10. Private Sub Test2(ByRef Vals As Variant)
    11.     Vals(0) = 10
    12. End Sub
    As to why you can't pass paramarrays byref in the first place - i don't know.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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...

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Sub Test2([B]ByRef[/B] Vals As Variant)
    2.     Vals(0) = 10
    3. End Sub
    But yes, the solution is to pass it by value or assign the ParamArray to a variant before passing it ByRef.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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?

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    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:
    1. Public Function InsertItem(ParamArray Values() As Variant) As String
    Then I can use it like this:
    VB Code:
    1. ucListViewTree1.DataList.InsertItem "c", "04", "aaa"
    2.  
    3.     ucListViewTree1.DataList.InsertItem "x", "05"

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