472,110 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

Passing A Reference Type ByVal....

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?
Nov 20 '05 #1
3 6529
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/de...guidelines.asp.

Hope this helps
Jay

"Scott M." <s-***@badspamsnet.net> wrote in message
news:u2**************@tk2msftngp13.phx.gbl... 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?

Nov 20 '05 #2
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
"Scott M." <s-***@badspamsnet.net> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
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?

Nov 20 '05 #3
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
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andy Read | last post: by
15 posts views Thread by Dave | last post: by
2 posts views Thread by Steve Turner | last post: by
2 posts views Thread by Witold Iwaniec via .NET 247 | last post: by
11 posts views Thread by John Pass | last post: by
4 posts views Thread by John Sheppard | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.