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

Why ByRef default changed to ByVal?

P: n/a
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)
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
* co********@yahoo.com (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.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3

P: n/a
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
"Carlos Gomez" <co********@yahoo.com> wrote in message
news:84**************************@posting.google.c om...
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)

Nov 20 '05 #4

P: n/a
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
"Carlos Gomez" <co********@yahoo.com> wrote in message
news:84**************************@posting.google.c om...
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)

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.