473,729 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6610
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******** ******@tk2msftn gp13.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******** ******@tk2msftn gp13.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
16843
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 Class
15
4680
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. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is that I get a segmentation error when doing so. I'm using the Dev-c++ compiler. Any ideas? ...
58
10164
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 code... TCHAR myArray; DoStuff(myArray);
39
7659
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
6
2845
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 types hold a reference to an object as opposed to object data itself. So, when reference type parameter is passed into a method, a copy of objects reference is passed in, so called method can do whatever to "original" object and a caller will see...
2
4844
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 cmd; short fdc; WORD dsf; short boxcar; short average; short chan_ena;
2
10271
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not Public Class ITest Private MyName As String Public TestId As String Public Sub New()
11
8127
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
4
5927
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 them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
9284
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.