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

paramarray

8 views
Skip to first unread message

peter

unread,
Jan 24, 2004, 10:26:05 AM1/24/04
to
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)
arg(0)=12
arg(1)="abc"
End Function

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

Who can help me?

Best regards

peter

Herfried K. Wagner [MVP]

unread,
Jan 24, 2004, 11:15:21 AM1/24/04
to
* "=?Utf-8?B?cGV0ZXI=?=" <anon...@discussions.microsoft.com> scripsit:

> 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)
> arg(0)=12
> arg(1)="abc"
> End Function

Mhm... Your function doesn't return anything. You may want to return a
new array... Have a look at the 'Return' keyword.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

peter

unread,
Jan 25, 2004, 8:06:07 AM1/25/04
to
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"
return 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.
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 (arg1,arg2,arg3)

Hope your suggestion.

John Hart [MSFT]

unread,
Jan 28, 2004, 3:37:15 PM1/28/04
to
Hi Peter,

As you have discovered the paramarray in VB.Net must be passed byVal which
as I'm sure you're aware means that you're actually passing a copy of the
values to the function. There for the original values will not be changed
even if you make changes in the function. See the following knowledge base
articles for more information about ParamArrays in VB.Net


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbup1003.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/
vblrfvbspec7_1_6_4.asp

Since it appears that you're also returning an object from the function one
solution for you will be to use an additional ByRef parameter that contains
the updated values. For example:

Public Function mytestmethod(ByRef ModifiedArgs() As Object, ByVal
ParamArray args() As Object) As Object
ModifiedArgs = args.Clone
ModifiedArgs(0) = 12
ModifiedArgs(1) = "abc"
Dim reValue As Object = "some value"

Return reValue

End Function

Sub mycalling()
Dim a As Integer = 66
Dim b As String = "zyz"
Dim r As Object
Dim NewValues() As Object

r = mytestmethod(NewValues, a, b)
a = NewValues(0)
b = NewValues(1)
MsgBox(a)
MsgBox(b)

End Sub

Hope that helps.
--
John Hart, Microsoft VB Team
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
> Thread-Topic: paramarray
> thread-index: AcPjQ/7JgZCJ5WZ/Q36zk8Pno1G1FQ==
> X-Tomcat-NG: microsoft.public.dotnet.languages.vb.upgrade
> From: "=?Utf-8?B?cGV0ZXI=?=" <anon...@discussions.microsoft.com>
> References: <8BA5FAA8-7797-4D47...@microsoft.com>
<OuLXIVp...@TK2MSFTNGP11.phx.gbl>
> Subject: Re: paramarray
> Date: Sun, 25 Jan 2004 05:06:07 -0800
> Lines: 29
> Message-ID: <ACFFAE45-9B6D-4E10...@microsoft.com>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="Utf-8"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> Content-Class: urn:content-classes:message
> Importance: normal
> Priority: normal
> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
> Newsgroups: microsoft.public.dotnet.languages.vb.upgrade
> Path: cpmsftngxa07.phx.gbl
> Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.languages.vb.upgrade:6123
> NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
> X-Tomcat-NG: microsoft.public.dotnet.languages.vb.upgrade

IanZ

unread,
Aug 11, 2009, 10:39:46 PM8/11/09
to
--------------------------------------- This Is Vb 6.0 Code --------------------------------------------------
/*This is The Caller*/
LookUpST(adcLEDGER, "SELECT Company_Code From Company_Profile WHERE Company_Name = '" & Trim(cboCOMPANY.Text) & "'", strCOMPANY_CODE)

/*This is the function*/

Public Function LookUpST(ByRef adcCON As ADODB.Connection, ByVal strSTT As String, Optional ByVal vntHasil As String = "", Optional ByVal vntRETURN As String = "", Optional ByVal vntNEXT As String = "", Optional ByVal vntNEXT_TO As String = "") As Boolean

On Error GoTo ErrHndl
Dim adrLook As New ADODB.Recordset
adrLook.CursorLocation = ADODB.CursorLocationEnum.adUseClient
adrLook.Open(strSTT, adcCON, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic)

If adrLook.RecordCount > 0 Then
adrLook.MoveFirst()
If Not IsDBNull(vntHasil) Then
vntHasil = CStr(IIf(IsNothing(adrLook(0)), Nothing, adrLook(0)).value)
End If

/* This is the part when The error Start*/
If Not IsDBNull(vntRETURN) Then
vntRETURN = CStr(IIf(IsNothing(adrLook(1)), Nothing, adrLook(1)).value)
End If
If Not IsDBNull(vntNEXT) Then
vntNEXT = CStr(IIf(IsNothing(adrLook(2)), Nothing, adrLook(2)).value)
End If
If Not IsDBNull(vntNEXT_TO) Then
vntNEXT_TO = CStr(IIf(IsNothing(adrLook(3)), Nothing, adrLook(3)).value)
End If
adrLook.Close()
LookUpST = True
Else
vntHasil = ""
'vntRETURN = ""
'vntNEXT = ""
'vntNEXT_TO = ""
LookUpST = False
End If

adrLook = Nothing
Exit Function

ErrHndl:
LookUpST = False
MsgBox(Err.Description, vbCritical, "Error")

End Function

The Error is = "Item cannot be found in the collection corresponding to the requested name or ordinal"

i think the error has been show because the variant function of Vreturn cannot be converted into vb.net maybe sombody can help me...

thanks before

From http://www.developmentnow.com/g/41_2004_1_0_0_151930/paramarray.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Armin Zingler

unread,
Aug 12, 2009, 7:39:06 AM8/12/09
to
IanZ schrieb:
> [...]

> vntHasil = CStr(IIf(IsNothing(adrLook(0)), Nothing, adrLook(0)).value)
> [...]

>
> The Error is = "Item cannot be found in the collection corresponding to the requested name or ordinal"
>
> i think the error has been show because the variant function of Vreturn cannot be converted into vb.net maybe sombody can help me...

You forgot to tell us in which line the error occurs. I guess it's one
of the "IIf"-lines trying to use an invalid index with the Fields
collection. I also guess that it's an ADODB error. Set a breakpoint at
the line adrLook.MoveFirst and inspect adrLook.Fields.Count. What does
it say?

In addition - I may be wrong because I currently can't test the code -
I'm not sure if the IIf line can work at all. Currently you are checking
whether "adrLook(0)" is Nothing.

First, do you want to check "adrLook(0)" or really "adrLook(0).value"?

Second, I think, none of them will ever be Nothing. I neither know how a
Fields collection can ever contain a "Nothing" item, nor how a database
field value can ever be Nothing; the latter could be Null (DBNull.Value)
only. I've just had a look at the ADODB Fields documentation but at
first glance it doesn't say anything about if an item can be Nothing.

Third, the IIf statement doesn't make sense. It looks if adrLook(0) is
Nothing. If it is, it returns Nothing, otherwise it return adrLook(0).
That means, it always returns adrLook(0), i.e. you could replace the
whole IIf part by "adrLook(0)".

Fourth, you didn't enable Option Strict yet. If you do it you will see
that the IIf function returns an Object. Not every object has a .value
property. What does IIf return? It's either Nothing or a field object.
Guess what happens if you want to access Nothing.value. Right, a
NullReferenceException occurs.


Can you also post the original VB6 code?


Armin

--
https://epetitionen.bundestag.de/index.php?action=petition;sa=details;petition=4958

0 new messages