issue of paramarray

G

Guest

Hi everyone

In VB6,paramarray parameter is passed using byref,but in VB.NET,using byval.How can I get back the changed value of the paramarray parameter

My work case in vb.net like this
----------------------------------------------------
Function mytestmethod(paramarray byval arg() as object) as objec
arg(0)=1
arg(1)="abc
dim revalue as object="somevalue
return revalue
End Functio

Sub mycalling(
dim a as integer=6
dim b as string="xyz
dim r as objec
r=mytestmethod (a,b
msgbox (a
msgbox (b
End Su
----------------------------------------------------
the expected value of a,b is 12,"abc";but it remains 66 or "xyz"

Who can help me

Best regard

pete
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
As was pointed out in the other newsgroups:

Short answer: You dont!

Long answer: Change the way you are doing it! As ByRef ParamArrays are not
supported! The ParamArray creates an array of the type given, an array holds
either values or objects, an array cannot hold references to the variable
itself.

I would consider either explicit byref parameters, or passing a single
array. However! I don't think I would create a function that had side
effects (modified its parameters). I would use a function when I needed to
return a single value. I would use a sub with byref if I needed to return
multiple values. Or, more then likely, I would use a function that return an
array or structure if I needed to return multiple values. More then likely
those "multiple values" were themselves a type (a class or structure).

A couple of possible solutions.
Function mytestmethod(byref arg1 as object, byref arg2 as object) as object

Function mytestmethod(byval arg() as object) as object
Sub mytestmethod(byref arg1 as object, byref arg2 as object, byref ret as
object)

Structure somestruct
dim a as integer=66
dim b as string="xyz"
End structure
Function mytestmethod() as somestruct
Sub mycalling()
dim a as integer=66
dim b as string="xyz"
dim r as object
r=mytestmethod (a,b)
msgbox (a)
msgbox (b)
Dim args(1) As object
mytestmethod(args)

I don't have a link handy, there was a long discussion about this with
possible workarounds a a couple of times in 2003. There was a discussion in
microsoft.public.dotnet.languages.vb.upgrade title "ByRef ParamArray
alternative" in late Oct 2003 where I refer to the original discussion, but
did not include the link to the original discussion. You may try
http://groups.google.com

Hope this helps
Jay

tom said:
Hi everyone,

In VB6,paramarray parameter is passed using byref,but in VB.NET,using
byval.How can I get back the changed value of the paramarray parameter?
 
G

Guest

Thanks for your response
I 'm sorry for my inattention.Now I correct it as follow

Function mytestmethod(paramarray byval arg() as object) as objec
arg(0)=1
arg(1)="abc
dim revalue as object="some value
mytestmethod=revalue
End Functio

Sub mycalling(
dim a as integer=6
dim b as string="xyz
dim r as objec
r=mytestmethod (a,b
msgbox (a
msgbox (b
End Su

You might misunderstand my intention.My requirement is as follow
1)in VB.NE
2)passing uncertain number of parameters to the called function and retrieve the modified value of these parameters
3)passing parameters via parameter list,just as
r=mytestmethod (arg1,arg2,arg3
not via array as
dim argarray() as object={arg1,arg2,arg3}
r=mytestmethod (argarray

Hope your suggestion

Best regard

to
 
J

Jon Skeet [C# MVP]

You might misunderstand my intention.My requirement is as follow:

<snip>

I don't think there was any misunderstanding there - it's just that the
answer was that your requirements as expressed don't "work" - they
can't be satisfied in VB.NET.
 
J

Jay B. Harlow [MVP - Outlook]

Tom,
You might misunderstand my intention.My requirement is as follow:
As Jon stated, I completely understand your intentions, you are attempting
to use a VB6 feature that is not supported in VB.NET.

Not supported is Not Supported!

Hence you need to find a different method! Or as Jon suggested give more
requirements on what you are really trying to do, so we can better offer
what the other methods may be.

BTW: asking the exact same question in multiple newsgroups then responding
with the exact same answer isn't really going to get you a different answer,
as ParamArray CANNOT be ByRef!
3)passing parameters via parameter list,just as
r=mytestmethod (arg1,arg2,arg3)
The "easiest" way is to define the parameters themselves as ByRef and not
use ParamArray!

Public Function mytestmethod (ByRef arg1 As Object,ByRef arg2 As
Object,ByRef arg3 As Object) As Object

Hope this helps
Jay

tom said:
Thanks for your response.
I 'm sorry for my inattention.Now I correct it as follow:

Function mytestmethod(paramarray byval arg() as object) as object
arg(0)=12
arg(1)="abc"
dim revalue as object="some value"
mytestmethod=revalue
End Function

Sub mycalling()
dim a as integer=66
dim b as string="xyz"
dim r as object
r=mytestmethod (a,b)
msgbox (a)
msgbox (b)
End Sub

You might misunderstand my intention.My requirement is as follow:
1)in VB.NET
2)passing uncertain number of parameters to the called function and
retrieve the modified value of these parameters.
 
C

Chris Botha

Declare your function just normal
Function mytestmethod(ByVal paramarray arg() as object) as object

When you call it, call it like this
Dim ObjArr(1) As Object
ObjArr(0) = 66
ObjArr(1) = "xyz"
mytestmethod(ObjArr)

Then when it returns, examine the values inside the obj array itself, they
changed.

tom said:
Hi everyone,

In VB6,paramarray parameter is passed using byref,but in VB.NET,using
byval.How can I get back the changed value of the paramarray parameter?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top