Why ByRef default changed to ByVal?

C

Carlos Gomez

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 changing
the original variable passed)
 
C

Cor

Hi Carlos,

Passing byval with the real value is only by values (integer, date, double
etc)

With an object it is always the value of the reference and not the object
itself.

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Carlos Gomez) scripsit:
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 changing
the original variable passed)

In practice, 'ByVal' makes more sense. Most types are reference types,
so passing them 'ByVal' doesn't make a big difference in matters of
what's done behind the scenes. 'ByVal' will copy a pointer, 'ByRef'
will pass it directly. 'ByVal' will prevent some mistakes because it's
not possible to change the reference of a variable which is passed into
a method in a 'ByVal' parameter. 'ByRef' makes sense for value types
(sometimes) if they are used as "output" parameters, and in some rare
cases for reference types too.
 
B

Brian Henry

ByRef is dangerous... if you change a value in a function that is passed
byref it will be changed for anything that uses it, byval makes a copy and
uses the copy leaveing the original variable uneffected... which you usually
dont want to do. Only use Byref when you need to access the object directly
that is being passed in, if you dont need to make direct changes to it, use
byval... it's taken basicly from the C++ standard and implemented in VB.net
 
J

Jay B. Harlow [MVP - Outlook]

Carlos,
In addition to the others comments:

Unfortunately the Ref word is overloaded here. :-|

Remember there are two types of Parameters (ByRef & ByVal) and there are two
types of variables (Reference Types & Value Types).

So you can have:
ByRef - Reference Type
ByRef - Value Type
ByVal - Reference Type
ByVal - Value Type

Lets look at types of variables:
When you define a Class you are defining a Reference Type. Which means that
a variable of this Class holds a reference to the object, the object itself
exists on the heap. If I assign this variable to a second variable a copy of
this reference is made and I now have two references to the same object on
the heap. There is still only one object on the heap. If you define a
Structure you are defining a Value Type. The variable itself holds the value
of the structure. If I assign this variable to a second variable a copy of
the entire structure is made. I now have two copies of the same structure.

Value Types include:
Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
with anything defined with the Structure or Enum keyword.
Value Types all derive directly or indirectly from System.ValueType

Reference Types include:
Object, and anything defined with the Class, Interface or Delegate keyword
are reference types.
Reference Types all derive from System.Object excluding types that inherit
from System.ValueType

Interface is a reference type, even if defined in a Structure. The structure
itself is a value type, however if you assign the structure to a Interface
variable, it will be Boxed, boxing places the value on the heap in a new
object (effectively making it a reference type)

Lets look at types of parameters:
Now when you define a parameter to be ByVal a copy of the variable is
passed. Remember Reference types hold a reference to the object, so passing
a Reference Type ByVal causes a copy of this reference to be passed as a
parameter, the single copy of the object itself is still on the heap. The
variable & parameter both have references to this single object. Passing a
Value Type ByVal causes a complete copy of the value to be passed as a
parameter. Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.

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

Hope this helps
Jay


Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top