Passing A Reference Type ByVal....

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

If I pass a reference type ByVal, am I making a copy of the object on the
heap or am I making a copy of a pointer to the object on the heap?

If I pass a string object (reference type) into a sub ByVal and modify it,
the original string doesn't change. If I pass it ByRef it does.

This makes it seem like the actual type (reference or value) that is being
passed is irrelevant and that only the ByVal or ByRef keyword matters.

Is this true?
 
Scott,
If I pass a reference type ByVal, am I making a copy of the object on the
heap or am I making a copy of a pointer to the object on the heap?
You are making a copy of the reference. There is only one object on the
heap.
If I pass a string object (reference type) into a sub ByVal and modify it,
the original string doesn't change.
You are changing the reference to the string you are not changing the
string. Remember Strings are Immutable!
If I pass it ByRef it does.
No the original String itself did not change, you changed a variable that
had a reference to the original string. The original string is still the
exact same value (unless of course the GC had it for lunch).

Consider the following:

Dim s1 As String = "Hello World"
Dim s2 As String = s1

Process(s2)

Public Sub Process(ByRef s As String)
s = "Change the string"
End Sub

If 'Process' had modified the object itself that s2 was referring to, the
value of s1 would have also changed, as they were both referring to the same
object. Process changed s2 to refer to a new object.
This makes it seem like the actual type (reference or value) that is being
passed is irrelevant and that only the ByVal or ByRef keyword matters.
The rule I go by is every thing is passed ByVal unless I know I need ByRef.
I only need ByRef when I need to change the variable of the calling routine.

NOTE: The pass ByRef for large structures rule is extremely rare as large
structures should be extremely rare per:
http://msdn.microsoft.com/library/d...genref/html/cpconvaluetypeusageguidelines.asp.

Hope this helps
Jay
 
the rules are as follows

1. if you pass a reference type byval you are creating a copy of the pointer
to the object in the stack.
both pointers point to a single instance of the object in the heap.

2. if you pass a reference type byref, you are using the original pointer in
the stack.

3. if you pass a value type byval, you are creating a copy of the variable
in the stack.
(changes will not affect original variable)

4. if you pass a value type byref, you are using the original value in the
stack.
( changes will affect original variable)

Kirk Graves
 
Hi Scott, Jay,

|| The rule I go by is everything is passed ByVal unless I
|| know I need ByRef. I only need ByRef when I need to
|| change the variable of the calling routine.

Likewise, and I make it clearly known in the documentation.

VB.NET has very sensibly made ByVal the default so that ByRefs can stick
out like a sore thumb.

Regards,
Fergus
 
Back
Top