Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with ParamArray

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I have a routine (rtn1) in which I pass an array of controls to it. This routine, in turn, passes that array to a 2nd routine (rn2).

Example:
Code:
Call rtn1 (Me, lstBox1)

Sub rtn1(frm as form, ParamArray varCtl() as variant)

    Dim ctl as control
    Dim i as integer

    for i = 0 to ubound(varCtl)
        set ctl = varctl(i)
    next i

    Call rtn2 (frm, varCtl)

End Sub

Sub rtn2(frm as form, ParamArray varCtl() as variant)

    Dim ctl as control
    Dim i as integer

    for i = 0 to ubound(varCtl)
        set ctl = varctl(i)(0)
    next i

End Sub
Note the difference in the 2 routines of how I have to reference the array. In the first routine I reference it as varctl(i). In the second routine I have to reference it as varctl(i)(0).

I need the 2nd routine to be able to reference it the same way. How do that?

 
How are ya FancyPrairie . . .

I query your use of [blue]ParamArray![/blue].
Microsoft said:
[blue]Using the ParamArray keyword [purple]enables a function to accept a variable number of arguments[/purple]. In the following definition, FirstArg is passed by value.[/blue]
Microsoft then provides the following example:
Code:
[blue]Function CalcSum(ByVal FirstArg As Integer, ParamArray OtherArgs())
   Dim ReturnValue
   
   [green]' If the function is invoked as follows:[/green]
   ReturnValue = CalcSum(4, 3, 2, 1)
   
   [green]' Local variables are assigned the following values: FirstArg = 4,
   ' OtherArgs(1) = 3, OtherArgs(2) = 2, and so on, assuming default
   ' lower bound for arrays = 1.[/green]

End Function[/blue]
Alternatively you can populate an array and pass the array using its name, like you would any other variable.

Also the following loop is pointless
Code:
[blue]for i = 0 to ubound(varCtl)
        [purple][b]set ctl = varctl(i)[/b][/purple]
    next i[/blue]
ctl is set to the last [purple]varctl(i)[/purple] of the loop.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
My example was a simplification of what I am trying to accomplish. My question concerned how to pass a paramArray from one routine to the next. And have both routines read it the same way (as stated in my first post).

The loop you questioned actually does things after the set and before the Next in my code. I can understand your concern about that code, but, again, I simplified my code so you could see the difference of how the variable had to be referenced (i.e. varctl(i) vs varctl(i)(0)).

Basically, I pass 0 to many control names to the routine. So it could look like:
Call rtn1 (frm)
or
Call rtn1 (frm, lstBox1)
or
Call rtn1 (frm, lstBox1, lstBox2, lstBox3)

But, I got around my problem by determining the number of dimensions of the variable varctl in rtn2.
 
If rtn2 is called only by rtn1:
Code:
Call rtn1(Me, lstBox1)

Sub rtn1(frm As Form, ParamArray varCtl() As Variant)
    Call rtn2(frm, varCtl)
End Sub

Sub rtn2(frm As Form, varCtl() As Variant)
    Dim ctl As Control
    Dim i As Integer
    For i = 0 To UBound(varCtl)
        Set ctl = varCtl(i)
    Next i
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top