ByVal vs ByRef 
Author Message
 ByVal vs ByRef

Person,

ByVal means you pass a copy of the value (a variable or object) to X (a
function or sub). ByRef means that you pass a pointer to the location in
memory that holds the actual value of the variable or object.

You must be careful with ByRef in that if you perform an invalid operation
on the value then your hosed because you have lost the original value of
your object or variable.

Quote:

> people.
> whats the difference between passing a variable to a function ByVal or
> ByRef?



Tue, 12 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef
An additional comment;
VB 5.0 the default parameter type is ByRef, so if you do not specify ByVal
in the Sub or Function parameter list, then you get ByRef (so be careful).
Quote:

> people.
> whats the difference between passing a variable to a function ByVal or
> ByRef?



Tue, 12 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef


Quote:
> people.
> whats the difference between passing a variable to a function ByVal or
> ByRef?

It's in the names ...  ByVal will transfer to *contents* of the variable to
the called Sub/Function.

The ByRef will transfer the *reference* (pointer to) the variable to the
called Sub/Function, enabeling the called function to change variables
stored in it's parents environment.

Or, in other words:  A ByValue transferred variable will, in the *callers*
point of view, stay *unchanged*, even if the called Sub/Function alters it.

A ByReference transferred variable will, in the *callers* point of view,
*change* if the called Sub/Function changes it.

Greetz,
  Rudy Wieser



Tue, 12 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef
See what others have written.  Also note that a parameter that is passed ByRef
can be changed to ByVal from the calling procedure by enclosing it in "( )"
(e.g. (intSomeValue)).  Also, it is my understanding the passing parameters
ByVal is faster than passing the reference (pointer) to the memory location of
the value.

Ralf

Quote:

>people.
>whats the difference between passing a variable to a function ByVal or
>ByRef?



Wed, 13 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef
Ralf,

Actually, in most cases ByRef is faster, since no new memory has to be
allocated to hold the data being passed only the space for the
reference address is allocated.

In the case of passing object references, all of which are passed
"ByRef" in the sense that only object references or pointers are
passed when passing instances of objects, passing ByVal will be
faster, if the object reference has to be marshalled across processes.
This is because there is more processing and allocation overhead in
stacking and marshalling the pointer to the reference, rather than
merely marshalling the value of a pointer.  If only one process is
used, the called function will run in-process with the calling
function, ByRef would then again be faster for passing object
references.

HTH,

Charlie

Quote:

>See what others have written.  Also note that a parameter that is passed ByRef
>can be changed to ByVal from the calling procedure by enclosing it in "( )"
>(e.g. (intSomeValue)).  Also, it is my understanding the passing parameters
>ByVal is faster than passing the reference (pointer) to the memory location of
>the value.

>Ralf


>>people.
>>whats the difference between passing a variable to a function ByVal or
>>ByRef?



Thu, 14 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef
I use ByVal extensively because it clearly documents the fact that the
parameter will not be modified by the sub or function to which it is passed.
This is especially handy when you revisit old code or are seeing another's
code for the first time.


Quote:
> Ralf,

> Actually, in most cases ByRef is faster, since no new memory has to be
> allocated to hold the data being passed only the space for the
> reference address is allocated.

> In the case of passing object references, all of which are passed
> "ByRef" in the sense that only object references or pointers are
> passed when passing instances of objects, passing ByVal will be
> faster, if the object reference has to be marshalled across processes.
> This is because there is more processing and allocation overhead in
> stacking and marshalling the pointer to the reference, rather than
> merely marshalling the value of a pointer.  If only one process is
> used, the called function will run in-process with the calling
> function, ByRef would then again be faster for passing object
> references.

> HTH,

> Charlie


> >See what others have written.  Also note that a parameter that is passed
ByRef
> >can be changed to ByVal from the calling procedure by enclosing it in
( )"
> >(e.g. (intSomeValue)).  Also, it is my understanding the passing
parameters
> >ByVal is faster than passing the reference (pointer) to the memory
location of
> >the value.

> >Ralf


> >>people.
> >>whats the difference between passing a variable to a function ByVal or
> >>ByRef?



Thu, 14 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef
   Also, if a string is passed "ByVal" it is passed with a null (CHR$(0))
automatically concantenated to it.


Sat, 16 Feb 2002 03:00:00 GMT  
 ByVal vs ByRef


Quote:
>    Also, if a string is passed "ByVal" it is passed with a null
(CHR$(0))
> automatically concantenated to it.

I just tested this and that is not the case. Apparently VB
differentiates between calls to DLL's and normal calls. So it appends a
CHR(0) if it's a call to a DLL, but doesn't if it's a call to a VB
function (and, I presume, sub).

--
Steven Salter, MCSD
Chronicle Technologies Inc.
http://www.chrontech.com

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Sat, 16 Feb 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. byVal vs ByRef

2. Byval vs Byref

3. Passing Parameters - A ByRef vs ByVal Question

4. ByVal vs. ByRef -- preformance

5. byval vs. byref

6. ByRef Vs ByVal

7. Override ByRef with ByVal

8. ByVal / ByRef

9. Questions regarding objects, assignments, byval, byref, etc...

10. Change Standard from ByVal to ByRef

11. Event Arguments : ByRef or ByVal (Which Best)?

12. Peformance in VB.NET - ByRef and ByVal

 

 
Powered by phpBB® Forum Software