473,395 Members | 1,474 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,395 software developers and data experts.

ByVal, ByRef - I'm confused

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 don't have a clue as
to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy
Nov 22 '05 #1
16 2419
Sandy,
I have gathered that ByVal makes a copy and
ByRef points to something and changes it. That is correct.
The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far? That is wrong.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters 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.

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

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl... 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 don't have a clue as
to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy

Nov 22 '05 #2
Sandy,
I have gathered that ByVal makes a copy and
ByRef points to something and changes it. That is correct.
The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far? That is wrong.

ByVal & ByRef Parameters are independent of Reference & Value Types. All
parameters 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.

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

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl... 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 don't have a clue as
to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy

Nov 22 '05 #3

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
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 don't have a clue as
to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy


First, you are correct in what you say above. So far so good.
There are two competing issues that might cause you to exercise your options
(ByVal or ByRef)
1. Data safety
2. Efficiency
Let's take the case of a simple integer:
Dim myInt as Integer
myInt = 33
If we pass myInt to a subprocedure (or function procedure) ByVal, we are
passing a copy of the contents of myInt (33). The subprocedure gets the
value 33, and if code in the subprocedure alters that value, the original
variable and its value are unaffected. The data in the original variable
remains safe from inadvertent alteration.
If we pass myInt to a subprocedure (or a function procedure) ByRef, the
subprocedure gets the address where the original variable is stored and can,
therefore, alter the contents of the original variable. If this happens
unintentionally, original data has been corrupted. If it's done
intentionally, it's a great way to modify multiple variables in a single
procedure (pass several ByRef).
Now, the efficiency consideration. Objects are passed ByRef as default,
because objects are often large. Making a copy of all the data in an object
is inefficient. When an object is passed ByRef, only a 4 byte hexadecimal
address is passed. Far more efficient. The safety of the original data is at
some risk to code in the procedure. The risk is minimized by well designed
classes which control access to data members in an object.
As primitive data types such as an integer are small, the efficiency
difference is minimal.
Bottom line: Unless you have good reasons, the defaults for primitives and
objects make sense.
You may want to write a few simple procedures and pass data to them both
ways, then modify the data in the procedure and see the effect on the
original parameters.

--
Peter - [MVP - .NET Academic]
Nov 22 '05 #4

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
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 don't have a clue as
to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a
hike!

Any help anyone can give me will be greatly appreciated!

Sandy


First, you are correct in what you say above. So far so good.
There are two competing issues that might cause you to exercise your options
(ByVal or ByRef)
1. Data safety
2. Efficiency
Let's take the case of a simple integer:
Dim myInt as Integer
myInt = 33
If we pass myInt to a subprocedure (or function procedure) ByVal, we are
passing a copy of the contents of myInt (33). The subprocedure gets the
value 33, and if code in the subprocedure alters that value, the original
variable and its value are unaffected. The data in the original variable
remains safe from inadvertent alteration.
If we pass myInt to a subprocedure (or a function procedure) ByRef, the
subprocedure gets the address where the original variable is stored and can,
therefore, alter the contents of the original variable. If this happens
unintentionally, original data has been corrupted. If it's done
intentionally, it's a great way to modify multiple variables in a single
procedure (pass several ByRef).
Now, the efficiency consideration. Objects are passed ByRef as default,
because objects are often large. Making a copy of all the data in an object
is inefficient. When an object is passed ByRef, only a 4 byte hexadecimal
address is passed. Far more efficient. The safety of the original data is at
some risk to code in the procedure. The risk is minimized by well designed
classes which control access to data members in an object.
As primitive data types such as an integer are small, the efficiency
difference is minimal.
Bottom line: Unless you have good reasons, the defaults for primitives and
objects make sense.
You may want to write a few simple procedures and pass data to them both
ways, then modify the data in the procedure and see the effect on the
original parameters.

--
Peter - [MVP - .NET Academic]
Nov 22 '05 #5
Peter van der Goes <p_**********@mars.cox.net> wrote:
Now, the efficiency consideration. Objects are passed ByRef as default,
No they're not. Objects aren't passed at all. References are passed
instead, which is different to passing an object itself by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
Bottom line: Unless you have good reasons, the defaults for primitives and
objects make sense.


Indeed they do - but they're not what you think they are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #6
Peter van der Goes <p_**********@mars.cox.net> wrote:
Now, the efficiency consideration. Objects are passed ByRef as default,
No they're not. Objects aren't passed at all. References are passed
instead, which is different to passing an object itself by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
Bottom line: Unless you have good reasons, the defaults for primitives and
objects make sense.


Indeed they do - but they're not what you think they are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Peter van der Goes <p_**********@mars.cox.net> wrote:
Now, the efficiency consideration. Objects are passed ByRef as default,


No they're not. Objects aren't passed at all. References are passed
instead, which is different to passing an object itself by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
Bottom line: Unless you have good reasons, the defaults for primitives and objects make sense.


Indeed they do - but they're not what you think they are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the terminology.
Nov 22 '05 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Peter van der Goes <p_**********@mars.cox.net> wrote:
Now, the efficiency consideration. Objects are passed ByRef as default,


No they're not. Objects aren't passed at all. References are passed
instead, which is different to passing an object itself by reference.

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
Bottom line: Unless you have good reasons, the defaults for primitives and objects make sense.


Indeed they do - but they're not what you think they are.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the terminology.
Nov 22 '05 #9
Dear Peter:

Thank you so much!

Sandy
-----Original Message-----

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
Peter van der Goes <p_**********@mars.cox.net> wrote:
> Now, the efficiency consideration. Objects are passed
ByRef as default,

No they're not. Objects aren't passed at all. References are passed instead, which is different to passing an object itself by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html for more information.
> Bottom line: Unless you have good reasons, the
defaults for primitives
and > objects make sense.
Indeed they do - but they're not what you think they

are.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the

terminology.

.

Nov 22 '05 #10
Dear Peter:

Thank you so much!

Sandy
-----Original Message-----

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
Peter van der Goes <p_**********@mars.cox.net> wrote:
> Now, the efficiency consideration. Objects are passed
ByRef as default,

No they're not. Objects aren't passed at all. References are passed instead, which is different to passing an object itself by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html for more information.
> Bottom line: Unless you have good reasons, the
defaults for primitives
and > objects make sense.
Indeed they do - but they're not what you think they

are.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the

terminology.

.

Nov 22 '05 #11
Dear Peter:

Thank you so much!

Sandy
-----Original Message-----

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
Peter van der Goes <p_**********@mars.cox.net> wrote:
> Now, the efficiency consideration. Objects are passed
ByRef as default,

No they're not. Objects aren't passed at all. References are passed instead, which is different to passing an object itself by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html for more information.
> Bottom line: Unless you have good reasons, the
defaults for primitives
and > objects make sense.
Indeed they do - but they're not what you think they

are.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the

terminology.

.

Nov 22 '05 #12
Dear Peter:

Thank you so much!

Sandy
-----Original Message-----

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
Peter van der Goes <p_**********@mars.cox.net> wrote:
> Now, the efficiency consideration. Objects are passed
ByRef as default,

No they're not. Objects aren't passed at all. References are passed instead, which is different to passing an object itself by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html for more information.
> Bottom line: Unless you have good reasons, the
defaults for primitives
and > objects make sense.
Indeed they do - but they're not what you think they

are.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Agreed. I should have been more careful with the

terminology.

.

Nov 22 '05 #13
Dear Jay:

Thanks so much! Why couldn't my books be that clear?

Sandy
-----Original Message-----
Sandy,
I have gathered that ByVal makes a copy and
ByRef points to something and changes it.That is correct.
The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far?

That is wrong.

ByVal & ByRef Parameters are independent of Reference &

Value Types. Allparameters by default are passed ByVal, you should only pass a parameterByRef when you have to, which is when you need to modify the callersvariable.

A Reference Type is an object that exists on the heap. If I have a variablethat is a reference type and assign the variable to another variable. Bothvariables 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 andI 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 valuesas a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable ismade. So for a Reference Type a copy of the reference is made, which meansthere 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 variableis made. So for a Reference Type you have a reference to a reference to theobject, for a Value Type you have a reference to the value.
Remember ByVal & ByRef are how parameters are passed. Reference & ValueTypes are how quantities are stored.

Hope this helps
Jay

"Sandy" <an*******@discussions.microsoft.com> wrote in messagenews:05****************************@phx.gbl...
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 don't have a clue as to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a hike!

Any help anyone can give me will be greatly appreciated!

Sandy

.

Nov 22 '05 #14
Dear Jay:

Thanks so much! Why couldn't my books be that clear?

Sandy
-----Original Message-----
Sandy,
I have gathered that ByVal makes a copy and
ByRef points to something and changes it.That is correct.
The default for
simple data types is ByVal and for objects, it's ByRef.
Am I correct so far?

That is wrong.

ByVal & ByRef Parameters are independent of Reference &

Value Types. Allparameters by default are passed ByVal, you should only pass a parameterByRef when you have to, which is when you need to modify the callersvariable.

A Reference Type is an object that exists on the heap. If I have a variablethat is a reference type and assign the variable to another variable. Bothvariables 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 andI 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 valuesas a copy was made.

Now when you pass a variable to a ByVal parameter a copy of the variable ismade. So for a Reference Type a copy of the reference is made, which meansthere 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 variableis made. So for a Reference Type you have a reference to a reference to theobject, for a Value Type you have a reference to the value.
Remember ByVal & ByRef are how parameters are passed. Reference & ValueTypes are how quantities are stored.

Hope this helps
Jay

"Sandy" <an*******@discussions.microsoft.com> wrote in messagenews:05****************************@phx.gbl...
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 don't have a clue as to when I might use which and for what purpose.

Don't know what my problem is . . . maybe my brain took a hike!

Any help anyone can give me will be greatly appreciated!

Sandy

.

Nov 22 '05 #15

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
Dear Peter:

Thank you so much!

Sandy


Sandy,
Be sure to study Jay's response, as it's far better than mine!

Nov 22 '05 #16

"Sandy" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
Dear Peter:

Thank you so much!

Sandy


Sandy,
Be sure to study Jay's response, as it's far better than mine!

Nov 22 '05 #17

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

Similar topics

3
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...
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
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,...
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: thomasp | last post by:
Will someone tell my the difference between ByRef and ByVal when passing values between subs? Thanks, Thomas -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service...
9
Frinavale
by: Frinavale | last post by:
Hi there! For the last few days I've been running my code through the code analyzer that comes with Visual Studio 2005. I've found a whole bunch of useful design recommendations produced by this...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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.