By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

ByVal, ByRef - I'm confused

P: n/a
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
Share this Question
Share on Google+
16 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.