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

Passing A Reference Type ByVal....

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.