473,473 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing ByRef and ByVal VB.NET

Dear all,

I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:

Public Class Person
Public Name as String
Public Age as Integer
End Class

Then pass a person object to a method 'ByVal'

Public Sub ChangePerson(ByVal Person as Person)
Person.Name = "MyName"
Person.Age = 22
End Sub

Why are the values Name and Age changed when I'm passing by value? I would
expect this behaviour when I'm passing by reference. I'm confused...(It's
not difficult!)

Any pointers to set me straight would be appreciated!

Thanks
Jul 19 '05 #1
3 16802
Andy

I'm not entirely sure how things work in .Net since I'm really a VB6
developer, but the same is true with VB6.

ByVal will not affect simple things like strings, but it does update the
object's properties. I'm not exactly sure of the reason, but I believe it's
to do with the fact that ByRef and ByVal are actually talking about whether
the object pointer is passed by reference or by value...the actual object
itself is a single object in memory. Passing an object pointer by val or by
ref is really meaningless (the default should be byVal)

Maybe someone can provide the "perfect" answer.

Griff
"Andy Read" <qz****@btinternet.com> wrote in message
news:bi**********@hercules.btinternet.com...
Dear all,

I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:

Public Class Person
Public Name as String
Public Age as Integer
End Class

Then pass a person object to a method 'ByVal'

Public Sub ChangePerson(ByVal Person as Person)
Person.Name = "MyName"
Person.Age = 22
End Sub

Why are the values Name and Age changed when I'm passing by value? I would expect this behaviour when I'm passing by reference. I'm confused...(It's
not difficult!)

Any pointers to set me straight would be appreciated!

Thanks

Jul 19 '05 #2
Hi Andy

Hopefully the following expansion of your example will illustrate the
difference.

Public Class CPerson
Public Name as String
Public Age as Integer
End Class

Public Sub ChangePerson1(ByVal Person as CPerson)
' These lines will always change Name and Age
Person.Name = "MyName"
Person.Age = 22

' The next line will have no effect on the caller
Person = New CPerson

End Sub

Public Sub ChangePerson2(ByRef Person as CPerson)
' These lines will always change Name and Age
Person.Name = "MyName"
Person.Age = 22

' This line will affect the caller
Person = New CPerson

End Sub

As you will see, the only difference between ChangePerson1 and ChangePerson2
is the ByVal / ByRef parameter.

In each case, the function is passed a CPerson object. In the first the
pointer to the object is passed by value, therefore it will not affect the
callers pointer to the object. In the second case the function is passed a
reference to this pointer. If you change it it will also change the caller's
pointer.

In essence, the object is always passed by reference (in a pointer) but the
pointer is the thing that is affected. The reason for this is that if the
entire object were passed by value then the function call would have to
perform a byte-by-byte copy of the object, and this could be a very costly
overhead for large objects.

HTH

Charles
"Andy Read" <qz****@btinternet.com> wrote in message
news:bi**********@hercules.btinternet.com...
Dear all,

I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:

Public Class Person
Public Name as String
Public Age as Integer
End Class

Then pass a person object to a method 'ByVal'

Public Sub ChangePerson(ByVal Person as Person)
Person.Name = "MyName"
Person.Age = 22
End Sub

Why are the values Name and Age changed when I'm passing by value? I would expect this behaviour when I'm passing by reference. I'm confused...(It's
not difficult!)

Any pointers to set me straight would be appreciated!

Thanks

Jul 19 '05 #3
Thanks to all for such speedy replies! And for giving such detailed
explanations.

"Andy Read" <qz****@btinternet.com> wrote in message
news:bi**********@hercules.btinternet.com...
Dear all,

I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:

Public Class Person
Public Name as String
Public Age as Integer
End Class

Then pass a person object to a method 'ByVal'

Public Sub ChangePerson(ByVal Person as Person)
Person.Name = "MyName"
Person.Age = 22
End Sub

Why are the values Name and Age changed when I'm passing by value? I would expect this behaviour when I'm passing by reference. I'm confused...(It's
not difficult!)

Any pointers to set me straight would be appreciated!

Thanks

Jul 19 '05 #4

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

Similar topics

39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
3
by: Scott M. | last post by:
If I pass a reference type ByVal, am I making a copy of the object on the heap or am I making a copy of a pointer to the object on the heap? If I pass a string object (reference type) into a sub...
12
by: Dennis D. | last post by:
Hello: I want a function to return three variables to the calling procedure: Private Function CalcTimes(ByVal iAddDays as Integer, ByVal iAddHours as Integer, ByVal iAddMins as Integer) As...
4
by: Erik Foreman | last post by:
this is what I have 'variables defined as arrays Dim ceday(), ceti(), ceto(), ceproj(), cenotes() As String Dim cerow As Int32
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...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
4
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
4
by: Scott M. | last post by:
Does C# support passing arguments ByVal & ByRef? If so, what is the default (ByVal?)? And, if so, how do you explicitly specify either? Thanks.
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.