473,396 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ByVal and ByRef in VB.Net

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
Jul 21 '05 #1
3 1313
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" <Mi****@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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

Jul 21 '05 #2
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" <Mi****@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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

Jul 21 '05 #3


"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

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
7
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
8
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.