byVal Vs. byRef

  • Thread starter Thread starter Rob Panosh
  • Start date Start date
R

Rob Panosh

Hello,

Ok here is the senerio:

.....

Dim myArrayList as New ArrayList(0)

me.Test_A( myArrayList )

myArralist.Count > 0 'This will be TRUE.

Public Sub Test_A( byVal X as ArrayList )

'Add three items.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

RETURN


When should I make the incoming parameter X as byRef?


Thanks,
Rob Panosh
 
that should be a byref if you are going to make changes to it that will be
visible outside of that sub... byval only makes a copy of the data.. byref
makes a pointer to the data, which is what you need if you are going to make
changes that are visible outside of that test sub, or they will just get
thrown out as the sub exits... and why do you have return in there?
 
Rob:

I'm not sure what you are asking, but an ArrayList is a Reference type, so
even if you pass it by value, all you are passing is a Copy of the REFERENCE
to the object. For all intents and purposes you won't notice the
difference. Yes, if you pass this reference type ByVal and the function
it's passed to makes any changes to it, the original ArrayList will be
changed. So the only difference between passing a Ref type byval or byRef
is that in the first case you pass a copy of the Reference, in the second
you pass the actual reference. If you want a unique copy that you can do
with what you please, you'll probalby need to opt for a deep clone of it.

HTH,

Bill
 
William,
even if you pass it by value, all you are passing is a Copy of the REFERENCE
to the object.
That is what I was asking. I could see a difference between them. So are
there any peformance implications here?
My guess byRef would have better performance because it doesn't have to make
a copied of the reference.

Thanks for your help and timely reponse

Rob
 
that should be a byref if you are going to make changes to it that will be
It doesn't make any difference how I pass it I can still see the changes.
thrown out as the sub exits... and why do you have return in there?
fat fingers .... didn't mean to put it there.

rob
 
Rob,

If Test_A was function and you were assigning an array list back to
myArrayList then ByVal is OK. If it's a sub and you want the original
object modified then pass it as ByRef. In this instance, Test_A only
creates a new ArrayList object (X), adds some items and then does nothing,
myArrayList isn't modified at all.

Hope this helps

Glen
 
Rob Panosh said:
My guess byRef would have better performance because it doesn't have
to make a copied of the reference.

Performance difference byval and byref when passing reference types is close
to zero (or _is_ zero). Both store a 4-byte value on the stack.

In a procedure, when accessing an object passed to the procedure, ByVal is a
little bit faster - but this is also close to zero (but is not zero).
 
Rob,
In addition to the others comments:
When should I make the incoming parameter X as byRef?
Short answer: Only when you need to modify the caller's variable!

Long answer:
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.

Less memory use & better performance should not be a factor in choosing
ByVal & ByRef. The only time to consider ByRef for less memory & performance
is when passing large structures (structures as in defined with the
Structure keyword), however structures should never be large!

Structure Usage Guidelines.
http://msdn.microsoft.com/library/d...genref/html/cpconvaluetypeusageguidelines.asp

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
 
Rob Panosh said:
William,

That is what I was asking. I could see a difference between them. So are
there any peformance implications here?
My guess byRef would have better performance because it doesn't have to make
a copied of the reference.

No. A reference is essentially a pointer. A 32-bit argument passed on the
stack. It doesn't get any cheaper than that.

David
 
Armin Zingler said:
Performance difference byval and byref when passing reference types
is close to zero (or _is_ zero). Both store a 4-byte value on the
stack.

Should be: ....difference between byval and byref...


In addition: The difference when passing value types depends on the size of
the value type. Passing a value type ByRef always stores 4 byte on the
stack. Passing it ByVal stores the whole object on the stack, so the bigger
the value type, the slower. But, as it has already been mentioned, the
decision to use ByVal or ByRef should not depend on this, but on whether the
passed *variable* should be changable (and the variable contains a reference
with reference types and the whole object with value types...)
 
Rob,

Basically, there are two types of variables: value types and reference
types. Value types consist simply of a value (integer, float etc.).
Reference types consist of two things - the object and the pointer to the
object (your variable name). When you pass a reference type by value, you
copy the pointer, not the object.

As the ArrayList type is a *reference* object, the variable you named
"myArrayList" is an object pointer that points to somewhere in memory that
contains your ArrayList.

When you pass "myArrayList" By Value to a function, the object pointer is
copied, but still points to the same object in memory - this is why you can
change the contents of the ArrayList. In your function, if you set "X" to a
new Arraylist, this will not be reflected by the "myArrayList" variable as
you passed it by value - it will still be the same ArrayList instance.

However, if you pass "myArrayList" by Reference, you pass the "myArrayList"
object pointer. Again, you can modify the contents, but this time, if you
set X to a new ArrayList, "myArrayList" will also be set to the new
ArrayList because you passed the object pointer by reference.

Hope this is slightly clearer than mud ;)

Trev.
 
Hello, Rob:

Codemonkey has given an excellent explanation. Here is a practical case:

'\\\
Public Sub Test_A( byVal X as ArrayList )

'Add three items when passed ByVal or ByRef.
X.Add( "Item 1")
X.Add( "Item 2")
X.Add( "Item 3")

'This will be useless using ByVal, but will change the original X when passed ByRef:
Dim Y As New ArrayList
Y.Add( "Item 4")
Y.Add( "Item 5")
Y.Add( "Item 6")
X=Y

End Sub
'///

Regards.


"Rob Panosh" <[email protected]> escribió en el mensaje | Hello,
|
| Ok here is the senerio:
|
| ....
|
| Dim myArrayList as New ArrayList(0)
|
| me.Test_A( myArrayList )
|
| myArralist.Count > 0 'This will be TRUE.
|
| Public Sub Test_A( byVal X as ArrayList )
|
| 'Add three items.
| X.Add( "Item 1")
| X.Add( "Item 2")
| X.Add( "Item 3")
|
| RETURN
|
|
| When should I make the incoming parameter X as byRef?
|
|
| Thanks,
| Rob Panosh
 
Back
Top