PC Review


Reply
Thread Tools Rate Thread

ByVal and ByRef in VB.Net

 
 
=?Utf-8?B?TWljaGVs?=
Guest
Posts: n/a
 
      1st Mar 2005
Hi all, I wrote this sample code :

Private Sub Button1_Click( ..... ) Handles Button1.Click
Dim x As New ArrayList
x.Add(1)
x.Add(1)
TestArrayList(x)
MsgBox("TestArrayList : " & x.Count.ToString)
End Sub

Private Sub TestArrayList(ByVal lst As ArrayList)
lst.Add(1)
lst.Add(1)
lst.Add(1)
End Sub

When I wrote it, I was sure to see in the message box that the ArrayList
have 2 items, because TestArrayList use the "lst" parameter ByVal. But, when
I ran that, I saw in the message box that the arraylist have 5 items....

Can you explain me what append here ?

Thanks a lot

Michel


 
Reply With Quote
 
 
 
 
Marina
Guest
Posts: n/a
 
      1st Mar 2005
ByVal refers to the variable. For objects, it means a copy of the pointer to
the object is passed, as opposed to the original pointer. Meaning if you
were to say lst = Nothing, the x variable would not be effected. If you were
to pass ByRef, 'x' would actually be Nothing after the function call.

Under no conditions would the entire object ever be copied - not only would
this be a performance and memory nightmare, but often times it would raise a
lot of questions about whether object that this object points to needs to be
copied, etc.

"Michel" <(E-Mail Removed)> wrote in message
news:1CF6D81A-B9FD-465D-A2B2-(E-Mail Removed)...
> Hi all, I wrote this sample code :
>
> Private Sub Button1_Click( ..... ) Handles Button1.Click
> Dim x As New ArrayList
> x.Add(1)
> x.Add(1)
> TestArrayList(x)
> MsgBox("TestArrayList : " & x.Count.ToString)
> End Sub
>
> Private Sub TestArrayList(ByVal lst As ArrayList)
> lst.Add(1)
> lst.Add(1)
> lst.Add(1)
> End Sub
>
> When I wrote it, I was sure to see in the message box that the ArrayList
> have 2 items, because TestArrayList use the "lst" parameter ByVal. But,
> when
> I ran that, I saw in the message box that the arraylist have 5 items....
>
> Can you explain me what append here ?
>
> Thanks a lot
>
> Michel
>
>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      1st Mar 2005
Because although you are passing the array ByVal, an array is a Reference
Type. When you pass a reference type ByVal, you are not passing a copy of
the reference type, but instead you are passing a copy of the pointer to the
original reference type. You wind up with 2 pointers (in your case: x and
lst) that point to just one object (your array).

Only when you pass a value type (integer, date, Boolean, decimal, etc.)
ByVal do you get a copy of the data to work with.

"Michel" <(E-Mail Removed)> wrote in message
news:1CF6D81A-B9FD-465D-A2B2-(E-Mail Removed)...
> Hi all, I wrote this sample code :
>
> Private Sub Button1_Click( ..... ) Handles Button1.Click
> Dim x As New ArrayList
> x.Add(1)
> x.Add(1)
> TestArrayList(x)
> MsgBox("TestArrayList : " & x.Count.ToString)
> End Sub
>
> Private Sub TestArrayList(ByVal lst As ArrayList)
> lst.Add(1)
> lst.Add(1)
> lst.Add(1)
> End Sub
>
> When I wrote it, I was sure to see in the message box that the ArrayList
> have 2 items, because TestArrayList use the "lst" parameter ByVal. But,
> when
> I ran that, I saw in the message box that the arraylist have 5 items....
>
> Can you explain me what append here ?
>
> Thanks a lot
>
> Michel
>
>



 
Reply With Quote
 
=?Utf-8?B?TWljaGVs?=
Guest
Posts: n/a
 
      1st Mar 2005


"Michel" wrote:

> Hi all, I wrote this sample code :
>
> Private Sub Button1_Click( ..... ) Handles Button1.Click
> Dim x As New ArrayList
> x.Add(1)
> x.Add(1)
> TestArrayList(x)
> MsgBox("TestArrayList : " & x.Count.ToString)
> End Sub
>
> Private Sub TestArrayList(ByVal lst As ArrayList)
> lst.Add(1)
> lst.Add(1)
> lst.Add(1)
> End Sub
>
> When I wrote it, I was sure to see in the message box that the ArrayList
> have 2 items, because TestArrayList use the "lst" parameter ByVal. But, when
> I ran that, I saw in the message box that the arraylist have 5 items....
>
> Can you explain me what append here ?
>
> Thanks a lot
>
> Michel
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
ByVal vs. ByRef eBob.com Microsoft VB .NET 9 20th Nov 2009 12:04 AM
byVal Vs. byRef Rob Panosh Microsoft VB .NET 11 16th Dec 2003 12:53 AM
ByVal and ByRef Goncalo Microsoft ASP .NET 1 4th Dec 2003 09:57 AM
ByRef or ByVal Hei Microsoft VB .NET 7 18th Nov 2003 03:42 PM
Is ByVal always better if ByRef isn't necessary Jeff Microsoft Excel Programming 5 25th Jul 2003 09:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 PM.