Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing a ParamArray argument to another function in VB6

548 views
Skip to first unread message

Erin Stanfill

unread,
Oct 12, 2005, 11:03:04 AM10/12/05
to
I have a Sub that takes a ParamArray argument. I want to pass the argument
to a function with the following signature:

Public Function ReplaceArgs(ByVal Text As String, _
ParamArray args() As Variant) As String

However, when I do this, args becomes a 2 dimensional array, with all of
the original arguments in the first "row" of the array. Is there anyway to
prevent this from happening.

Thanks,

Erin

Ken Halter

unread,
Oct 12, 2005, 11:23:25 AM10/12/05
to
One way would be to transfer the paramarray to a new array and pass that...
'=======
Option Explicit

Public Function ReplaceArgs(ByVal Text As String, _
ParamArray args() As Variant) As String

Dim vNewArray As Variant
vNewArray = args
Call MySub(vNewArray)

End Function

Private Sub MySub(SomeArgs As Variant)
Dim i As Integer
For i = 0 To UBound(SomeArgs)
Debug.Print SomeArgs(i)
Next
End Sub

Private Sub Command1_Click()
Call ReplaceArgs("SomeText", "This", "That", "The Other")
End Sub
'=======

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..

"Erin Stanfill" <erin.stan...@gmail.com> wrote in message
news:op.syjf3ecwjgucr1@chris...

Rick Rothstein [MVP - Visual Basic]

unread,
Oct 12, 2005, 11:30:22 AM10/12/05
to

Here is some sample code that passes the contents of a parameter
array into another function for processing (note the use of the
intermediate array). See if this helps you any.

Private Sub Form_Load()
Debug.Print MainFunction(1, 2, 3, 4, 5)
End Sub

Function MainFunction(ParamArray Stuff()) As Long
Dim IntermediateArray() As Variant
IntermediateArray = Stuff
MainFunction = CountThem(IntermediateArray())
End Function

Function CountThem(NormalArrayIn()) As Long
CountThem = UBound(NormalArrayIn) - LBound(NormalArrayIn) + 1
End Function

Rick


Larry Serflaten

unread,
Oct 12, 2005, 3:20:28 PM10/12/05
to

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote

> One way would be to transfer the paramarray to a new array and pass that...

But you loose the ByRef attribute of all the elements. About the only way
to pass a parameter array, while preserving all of its attributes is to re-create
the array as needed:

Private Sub Form_Load()
Dim a, b
a = 10
b = 10
Sub1 a, b
Debug.Print a, b ' Prints 1 2
End Sub

Sub Sub1(ParamArray X())
X(0) = 1 ' ByRef assignment
Select Case UBound(X)
Case 0
Sub2 X(0)
Case 1
Sub2 X(0), X(1)
' etc...
End Select
End Sub

Sub Sub2(ParamArray Y())
Y(1) = 2 ' ByRef assignment
End Sub


Ken Halter

unread,
Oct 12, 2005, 4:27:35 PM10/12/05
to
"Larry Serflaten" <serf...@usinternet.com> wrote in message
news:OWFeeE2z...@TK2MSFTNGP15.phx.gbl...

>
> But you loose the ByRef attribute of all the elements. About the only way
> to pass a parameter array, while preserving all of its attributes is to
> re-create
> the array as needed:
>

Ah... true. I knew there had to be a reason that, every time I've considered
using ParamArray, I chose not to <g> There's always one "gotchya" that makes
me decide to use "something else" instead.

0 new messages