473,387 Members | 3,787 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,387 software developers and data experts.

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 16798
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.