473,508 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2426
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
16813
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
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...
7
5491
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
12290
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
2486
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
1510
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
2445
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
10259
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
3261
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
17523
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
7229
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
7129
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
7333
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
7398
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
7061
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
7502
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...
0
5637
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,...
0
4716
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
1566
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 ...

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.