ByVal, ByRef - I'm confused

  • Thread starter Thread starter Sandy
  • Start date Start date
S

Sandy

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
 
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 said:
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 van der Goes said:
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.
 
Back
Top