473,545 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Differance between ByVal and BeRef when handling objects

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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

Nov 20 '05 #1
14 1517
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a
ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the
class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var
was pointing at.

I have modified the code below to show the difference. Run it and check the
results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class
Nov 20 '05 #2
Hi Niklas,

You can almost forever use ByVal
It is with a value the value itself and with an existing object the value of
the reference of the object.

Above I write already when it cannot be used ByVal, that is when the object
is declared however not created yet. Than you have to do it for an object
ByRef.

I hope this helps?

Cor
Nov 20 '05 #3
Thank you. That means that I have to redesign my application because the Set part of Property do not allow ByRef only ByVal...maybe I use a field.
Regards
/Niklas

"Shiva" wrote:
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a
ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the
class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var
was pointing at.

I have modified the code below to show the difference. Run it and check the
results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

Nov 20 '05 #4
Niklas,
Why would you need to redesign your app?

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable. Property Set routines should not be modifying the caller's
variable!

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay
"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:9B******** *************** ***********@mic rosoft.com...
Thank you. That means that I have to redesign my application because the Set part of Property do not allow ByRef only ByVal...maybe I use a field. Regards
/Niklas

"Shiva" wrote:
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var was pointing at.

I have modified the code below to show the difference. Run it and check the results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

Nov 20 '05 #5
I used to use this as an interview question...

When you're passing an object to a method - you're passing a variable
which points to the object. Both ByVal or ByRef pass a variable
which points to the object since a copy of an address is still the
valid address, so the object modifications persists upon leaving the
method. All ByVal does in this case is prevent you from changing the
variable pointing to the object to point to another object upon
leaving the method.

Nov 20 '05 #6
Are you sure that when you pass an array ByVal that a copy is made of the entire array?
--
Dennis in Houston
"Shiva" wrote:
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a
ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the
class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var
was pointing at.

I have modified the code below to show the difference. Run it and check the
results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

Nov 20 '05 #7
Thanks all of you. I did some test on my application and ByVal did the job.
Regards
/Niklas

"Jay B. Harlow [MVP - Outlook]" wrote:
Niklas,
Why would you need to redesign your app?

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters in VB.NET by default are passed ByVal, you should only pass a
parameter ByRef when you have to, which is when you need to modify the
callers variable. Property Set routines should not be modifying the caller's
variable!

A Reference Type is an object that exists on the heap. If I have a variable
that is a reference type and assign the variable to another variable. Both
variables will be pointing to the same object on the heap.

Dim x As Person
x = New Person()
Dim y As Person
y = x

Both x & y are the exact same Person object on the heap.

A Value Type does not live on the Heap. If I have a value type variable and
I assign it to another variable, a copy of the value is made.

Dim x As Integer
x = 100
Dim y As Integer
y = x

Although both x & y have the value 100, they are physically different values
as a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable is
made. So for a Reference Type a copy of the reference is made, which means
there is still only one object on the heap & two references to that object.
For a Value Type a copy of the value is made.

When you pass a variable to a ByRef parameter a reference to that variable
is made. So for a Reference Type you have a reference to a reference to the
object, for a Value Type you have a reference to the value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Hope this helps
Jay
"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:9B******** *************** ***********@mic rosoft.com...
Thank you. That means that I have to redesign my application because the

Set part of Property do not allow ByRef only ByVal...maybe I use a field.
Regards
/Niklas

"Shiva" wrote:
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var was pointing at.

I have modified the code below to show the difference. Run it and check the results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class


Nov 20 '05 #8
Hi,

I meant a 'copy of the reference' (address) is made not what the ref points
to.

"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:D8******** *************** ***********@mic rosoft.com...
Are you sure that when you pass an array ByVal that a copy is made of the
entire array?
--
Dennis in Houston
"Shiva" wrote:
Hi,

The output you are getting is indeed correct.

The way ByVal & ByRef differs in case of reference type is like this: When a ref type variable is passed ByVal, a copy of the reference var is passed
(ie, two vars for the same object). So, if you assign a new instance of the class inside the called method to the ByVal var, the original remains
intact. On the other hand, ByRef passes the reference var as is. Any new
reference assignment in the called method will change what the original var was pointing at.

I have modified the code below to show the difference. Run it and check the results.

"Niklas" <Ni****@discuss ions.microsoft. com> wrote in message
news:18******** *************** ***********@mic rosoft.com...
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 something?
Regards
/Niklas

Public Class Main
Shared Sub Main()
Dim testPropObj As New MyPropertObject
testPropObj.MyI nt = 1
Console.WriteLi ne("Org: testPropObj.MyI nt = " & testPropObj.MyI nt)
ChangeObjectByV al(testPropObj)
Console.WriteLi ne("ByVal: testPropObj = " & testPropObj.MyI nt)
ChangeObjectByR ef(testPropObj)
Console.WriteLi ne("ByRef: testPropObj = " & testPropObj.MyI nt)
Console.WriteLi ne("Press Enter to exit...")
Console.ReadLin e()
End Sub

Public Shared Sub ChangeObjectByV al(ByVal myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 5
End Sub

Public Shared Sub ChangeObjectByR ef(ByRef myObject As MyPropertObject )
myObject = New MyPropertObject
myObject.MyInt = 6
End Sub
End Class

Public Class MyPropertObject
Public MyInt As Integer
End Class

Nov 20 '05 #9
Yea I've wondered the same thing. Duplicating a large array sounds
prohibitively expensive. Maybe the byref and byvalue or just
specifying permission to write ????

Forrest

On Thu, 22 Jul 2004 19:00:03 -0700, Dennis
<De****@discuss ions.microsoft. com> wrote:
Are you sure that when you pass an array ByVal that a copy is made of the entire array?

Nov 20 '05 #10

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

Similar topics

8
10779
by: Nige | last post by:
Is it possible to create an alert box when a radio button is selected? I have a group of three, and I want a different alert for each one. -- Nige Please replace YYYY with the current year ille quis mortem cum maximus ludos, vincat
8
383
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 points to something and changes it. The default for simple data types is ByVal and for objects, it's ByRef. Am I correct so far? If so, I still...
10
452
by: Ptbrady | last post by:
What does "ByVal" mean? I couldn't find it in the Access help messages. Here is the first line of a function: Public Function GetAge(ByVal pvarBirthdate As Variant, ByVal pvarAgeCalcDate As Variant) As Long Why is ByVal needed? I've written other functions that don't have it, that is, they would look like: Public Function...
2
1593
by: Harshankumar | last post by:
Hello, I have the following questions 1) can we pass the parameters to interrupt? 2)can we return the parameters from interrupt Regards harshan
14
2492
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 allows me to store a reference to the object I passed by val, to change that object and for the change to be reflected in the callers copy of the...
2
10262
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not ...
11
8101
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub...
68
5168
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
7
9281
by: prefersgolfing | last post by:
In 6, by default arguments, were passed byref what is the default in .NET? Can you point me to any MSDN documentation? TIA
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
5982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3465
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
muto222
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.