473,394 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 6592
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Read | last post by:
Dear all, I thought I understood passing parameters ByVal and ByRef but I clearly don't! If I define a simple class of: Public Class Person Public Name as String Public Age as Integer End...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.