Passing Paramarray to another paramarray

  • Thread starter Thread starter Tubs
  • Start date Start date
T

Tubs

I know it might seem weird but i have a need to pass the contents of a
paramarray from one sub to another that also takes a paramarray.
Problem is i want to pass it exactly as it came in to the first one.
In other words, in the second one i want to refer to them as
arg1...argN. Currently if i pass it directly, it embeds itself into
another deeper structure. Is there any way to pass it the same way it
came in, as individual args to the second param array?
 
* (e-mail address removed) (Tubs) scripsit:
I know it might seem weird but i have a need to pass the contents of a
paramarray from one sub to another that also takes a paramarray.
Problem is i want to pass it exactly as it came in to the first one.
In other words, in the second one i want to refer to them as
arg1...argN. Currently if i pass it directly, it embeds itself into
another deeper structure. Is there any way to pass it the same way it
came in, as individual args to the second param array?

Why not create an overloded method that accepts an array?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
Tubs,
Is this a VB6 question?

Within VB.NET if you pass a ParamArray to a second ParamArray it simply
works. In other words VB.NET does not "embeds itself into another deeper
structure"!

I seem to remember VB6 would not handle this well.

The following VB.NET 2003 program behaves as expected:

Public Sub Main()
Test("a", "b", "c")
End Sub

Public Sub Test(ByVal ParamArray args() As String)
DoTest(args)
End Sub

Public Sub DoTest(ByVal ParamArray args() As String)
For Each arg As String In args
Debug.WriteLine(arg, "arg")
Next
End Sub

In that DoTest will print the three parameters passed to Test.

Note if you use the VS.NET 2002 syntax on the For Each, it works in VS.NET
2002 also!

Hope this helps
Jay
 
Herfried,
Because ParamArray parameters already accept the array! :-) See my other
post for details, try it.

The first time I did it, it was like "Wow! they got that right!!!!", as in
C++ & VB6 it was a pain as Tubs is suggesting. Which is where I wonder if
Tubs is using VB6.

It is also very handy that you can actually type the ParamArray, so you can
do efficient overloaded functions such as Min and Max.

Function Min(ParamArray As Integer) As Integer
Function Min(ParamArray As Long) As Long
Function Min(ParamArray As Double) As Double

I'm anxious to try Generic Functions that accept ParamArrays ;-)

Jay
 
* "Jay B. Harlow said:
Herfried,
Because ParamArray parameters already accept the array! :-) See my other
post for details, try it.

You are right...

\\\
Private Function Foo(ByVal ParamArray Bar() As Integer)
FooBar(Bar)
End Function

Private Function FooBar(ByVal ParamArray Bar() As Integer)
...
End Function
///

will work.

I'll have a look at your other post now.

;-)
 
unfortunately it is about VB6. I am glad to know dotnet got it right
because i need this there as well but this particular project is in
legacy code. Any thoughts for it? I have even tried repacking the
thing with no avail.

Troy B. Stauffer
Jack of all trades, master of none :)
 
Troy,
Have you considered 'faking' an overload? Which is effectively what Herfried
suggested.

Define a routine that accepts an array, that both ParamArray routines then
call.

Something like:

Public Sub Foo(ParamArray args() As Variant)
DoBar args
End Sub

Public Sub Bar(ParamArray args() As Variant)
DoBar args
End Sub

Private Sub DoBar(ByVal args As Variant)
' do real work for bar in here
Dim arg As Variant
For Each arg In args
Debug.Print arg
Next
End Sub

As this is effectively what you need to do in C++.

The problem then becomes do you call DoBar, or Bar. Which is where real
overloading in VB.NET is handy!

Hope this helps
Jay
 
No but i will give it a shot. I actual do something like this now just
have problems because there are multiple ways to call it. Thanks for
the advice though. If nothing else i learned about dotnet, which is
where i really enjoy being anyhow. :)

Troy B. Stauffer
Jack of all trades, master of none :)
 
Troy,
I should add that the other option is to ask in one of the other VB
newsgroups. This one is a VB.NET newsgroup, try one of the VB groups, that
do not have dotnet in the name. such as
microsoft.public.vb.general.discussion.

Hope this helps
Jay
 
Back
Top