Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Byval vs. byref

Reply
Thread Tools

Byval vs. byref

 
 
Iams
Guest
Posts: n/a
 
      02-24-2005
Forgive my newbie question, but I guess I don't understand passing
parameters, in my case an array.

I have a procedure which calls another procedure, passing an array. The new
procedure creates the array, but when it returns to the original procedure,
the array is lost, along with all the parameters I want modified by the
called procedure. I thought I understood the default was byref, which would
modify the actual variable, not just a copy. I haven't had this problem
before. How should I do this?

Dim arrClients as ArrayList
Private Sub DoStuff()
'do other stuff
Call CreateClientArray(arrClients, i, v, q)
End Sub

Public Class OtherStuff
Public Sub CreateClientArray(arrClients, i, v, q)
arrClients.Add("Client1")
arrClients.Add("Client2")
i = 1
v = "stuff"
q = "quarter"
end sub


 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      02-24-2005
The default way that arguments are passed in VB.NET is ByVal not ByRef.


"Iams" <Iams@hotmail> wrote in message
news:(E-Mail Removed)...
> Forgive my newbie question, but I guess I don't understand passing
> parameters, in my case an array.
>
> I have a procedure which calls another procedure, passing an array. The
> new
> procedure creates the array, but when it returns to the original
> procedure,
> the array is lost, along with all the parameters I want modified by the
> called procedure. I thought I understood the default was byref, which
> would
> modify the actual variable, not just a copy. I haven't had this problem
> before. How should I do this?
>
> Dim arrClients as ArrayList
> Private Sub DoStuff()
> 'do other stuff
> Call CreateClientArray(arrClients, i, v, q)
> End Sub
>
> Public Class OtherStuff
> Public Sub CreateClientArray(arrClients, i, v, q)
> arrClients.Add("Client1")
> arrClients.Add("Client2")
> i = 1
> v = "stuff"
> q = "quarter"
> end sub
>
>



 
Reply With Quote
 
 
 
 
Derek Harmon
Guest
Posts: n/a
 
      02-24-2005
"Iams" <Iams@hotmail> wrote in message news:(E-Mail Removed)...
> I thought I understood the default was byref, which would modify the
> actual variable, not just a copy.


Your understanding of the difference between ByVal and ByRef is correct;
except that the default is ByVal.

: :
> Public Sub CreateClientArray(arrClients, i, v, q)

Public Sub CreateClientArray( ByRef arrClients, i, v, q)

Here arrClients is passed-by-reference, whereas i, v and q
are passed-by-value.

Changes to arrClients in the Sub, CreateClientArray, affects
the variable arrClients in DoStuff( ) [it would even affect that
variable if it had a different name].

If you have any other questions on the subject, this MSDN Library
article explains everything,

http://msdn.microsoft.com/library/en...sMechanism.asp

> Forgive my newbie question, but I guess I don't understand passing
> parameters, in my case an array.


By the way, just a gentle nudge that VB.NET questions are best raised
in the microsoft.public.dotnet.languages.vb newsgroup. I've pointed
follow-up to that forum because it's probably a better place to discuss
this question than a newsgroup on ASP.NET (WebForms applications).


Derek Harmon


 
Reply With Quote
 
Malik Asif Joyia
Guest
Posts: n/a
 
      02-24-2005
Hello
Dear Iams

in Vb.NET parameters are by default passed by Value not by Reference,
you should Write a function with parameters like


Public Sub abc(ByRef a as long)
' ' here you parameter a is available by reference .

end Sub


Kind Regards
Malik Asif



"Iams" <Iams@hotmail> wrote in message
news:(E-Mail Removed)...
> Forgive my newbie question, but I guess I don't understand passing
> parameters, in my case an array.
>
> I have a procedure which calls another procedure, passing an array. The

new
> procedure creates the array, but when it returns to the original

procedure,
> the array is lost, along with all the parameters I want modified by the
> called procedure. I thought I understood the default was byref, which

would
> modify the actual variable, not just a copy. I haven't had this problem
> before. How should I do this?
>
> Dim arrClients as ArrayList
> Private Sub DoStuff()
> 'do other stuff
> Call CreateClientArray(arrClients, i, v, q)
> End Sub
>
> Public Class OtherStuff
> Public Sub CreateClientArray(arrClients, i, v, q)
> arrClients.Add("Client1")
> arrClients.Add("Client2")
> i = 1
> v = "stuff"
> q = "quarter"
> end sub
>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      02-24-2005
And, just to make it interesting...

If you pass a reference type (a class for example) ByVal (the default), you
WON'T get a copy of that object, you will get a copy of the pointer to that
object.


"Scott M." <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> The default way that arguments are passed in VB.NET is ByVal not ByRef.
>
>
> "Iams" <Iams@hotmail> wrote in message
> news:(E-Mail Removed)...
>> Forgive my newbie question, but I guess I don't understand passing
>> parameters, in my case an array.
>>
>> I have a procedure which calls another procedure, passing an array. The
>> new
>> procedure creates the array, but when it returns to the original
>> procedure,
>> the array is lost, along with all the parameters I want modified by the
>> called procedure. I thought I understood the default was byref, which
>> would
>> modify the actual variable, not just a copy. I haven't had this problem
>> before. How should I do this?
>>
>> Dim arrClients as ArrayList
>> Private Sub DoStuff()
>> 'do other stuff
>> Call CreateClientArray(arrClients, i, v, q)
>> End Sub
>>
>> Public Class OtherStuff
>> Public Sub CreateClientArray(arrClients, i, v, q)
>> arrClients.Add("Client1")
>> arrClients.Add("Client2")
>> i = 1
>> v = "stuff"
>> q = "quarter"
>> end sub
>>
>>

>
>



 
Reply With Quote
 
Iams
Guest
Posts: n/a
 
      02-24-2005
Thanks all, that explains it! And thanks for the pointer info.


"Iams" <Iams@hotmail> wrote in message
news:(E-Mail Removed)...
> Forgive my newbie question, but I guess I don't understand passing
> parameters, in my case an array.
>
> I have a procedure which calls another procedure, passing an array. The

new
> procedure creates the array, but when it returns to the original

procedure,
> the array is lost, along with all the parameters I want modified by the
> called procedure. I thought I understood the default was byref, which

would
> modify the actual variable, not just a copy. I haven't had this problem
> before. How should I do this?
>
> Dim arrClients as ArrayList
> Private Sub DoStuff()
> 'do other stuff
> Call CreateClientArray(arrClients, i, v, q)
> End Sub
>
> Public Class OtherStuff
> Public Sub CreateClientArray(arrClients, i, v, q)
> arrClients.Add("Client1")
> arrClients.Add("Client2")
> i = 1
> v = "stuff"
> q = "quarter"
> end sub
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cTor argument byVal or byRef ManicQin C++ 7 08-02-2008 09:38 PM
Byref / Byval? Rob Meade ASP .Net 5 12-28-2006 09:15 PM
custvalidator: why is args byval and not byref? xamman ASP .Net 1 12-22-2006 08:21 PM
ByVal and ByRef Goncalo ASP .Net 1 12-04-2003 09:57 AM
Re: How to set default to TRUE: Overrides Function OnBubbleEvent(ByVal source As Object, ByVal args As EventArgs) As Boolean? David Waz... ASP .Net 0 07-04-2003 07:32 PM



Advertisments