473,775 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about passing an object by reference

Hello all,

I'm passing a reference to a class into the constructor of a form, like
so: public MyForm(int count, ref Area myArea) {...}

How can I use myArea outside the constructor? Should I create another
Area and assign myArea to it (ie: Area foo = myArea;) or is there a
better way?

~AF

Nov 15 '05
13 17931
Jon,

You are right. The reference is passed by value, which is different from
passing by reference. I went back to my reference book on the subject (The
study of programming language -- Ryan Stansifer) and it confirms what you
say and what you write in your HTML document (and what the .NET
documentation says).

But rather than say, "no" when people says that objects are passed by
reference, I think that you should say "almost" or "not quite", because they
don't always grasp the difference between "passed by reference" and
"reference passed by value", and then, they get confused.

The most important point, as far as objects are concerned, is that the
reference is being passed around, and that the objects are never copied. So,
people don't need to use the "ref" keyword most of the time when they pass
an object because the reference is passed anyway (by value). The "ref"
keyword is only useful if they want the method to "return" a result (another
reference) through the parameter, which is not the usual case.

Bruno.
"Jon Skeet" <sk***@pobox.co m> a écrit dans le message de
news:MP******** *************** *@news.microsof t.com...
Ming Chen [.NET MVP] <qq******@yahoo .com> wrote:
I'm not getting the context of the discussion. Here is just what I think
about your questions.
Generally speaking, ValueTypes are passed by value, while Reference types are passed by reference by default.


No they are not. With reference types, the reference is passed by
value, which is a different thing to passing by reference.
See http://www.pobox.com/~skeet/csharp/parameters.html (again!)
For value types, it makes a lot sense to pass them by ref. Cause that's the only way to change the parameter object value in a function.
For reference types, it still gives some bonus to pass them by ref.


... which suggests that you *must* be wrong above, as if they are
passed by reference to start with, how could passing them by reference
give them some bonus?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #11
Bruno Jouhier [MVP] <bj******@clu b-internet.fr> wrote:
You are right. The reference is passed by value, which is different from
passing by reference. I went back to my reference book on the subject (The
study of programming language -- Ryan Stansifer) and it confirms what you
say and what you write in your HTML document (and what the .NET
documentation says).

But rather than say, "no" when people says that objects are passed by
reference, I think that you should say "almost" or "not quite", because they
don't always grasp the difference between "passed by reference" and
"reference passed by value", and then, they get confused.
To be fair, I *always* now give the URL to the more detailed
explanation. I'm trying to make the point that it really is quite a
sharp distinction in theoretical terms, even if a lot of the time they
end up having similar effects.
The most important point, as far as objects are concerned, is that the
reference is being passed around, and that the objects are never copied. So,
people don't need to use the "ref" keyword most of the time when they pass
an object because the reference is passed anyway (by value). The "ref"
keyword is only useful if they want the method to "return" a result (another
reference) through the parameter, which is not the usual case.


Indeed. And the fact that people *think* that they want to pass things
by reference when actually they want to pass a reference by value shows
how close things are in people's minds. By posting "almost" or "not
quite" there's an implication (IMO) that they really *are* quite close
together, whereas I'm trying to stress how different they are.

I'll think about it a bit more though, and maybe a bit less harsh :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #12


Ming Chen [.NET MVP] wrote:
I'm not getting the context of the discussion. Here is just what I think
about your questions.
Generally speaking, ValueTypes are passed by value, while Reference types
are passed by reference by default.
No.. that's politically (and logically) incorrect. Reference types are
passed by value.. since reference type vars hold the reference to the
object, their reference is copied accross.
-Andre

For value types, it makes a lot sense to pass them by ref. Cause that's the
only way to change the parameter object value in a function.
For reference types, it still gives some bonus to pass them by ref. For
example:
void foo1(object o) {
o = new Object(); //foo1(myo); this won't effect the original
"o" (myo).
}
void foo2(ref object o) {
o = new Object(); //foo2(myo); this does assign a new object to
the calling object (myo).
}

Just my 2 cents. Hope it helps.
Ming Chen

"G" <gl****@bigfoot .com> wrote in message
news:bg******** **@sparta.btint ernet.com...
So what is the outcome of this?
Objects are always passed by reference or value?



Nov 15 '05 #13
Ahh.. you guys already got to it :) I was hoping you would.

-Andre

Jon Skeet wrote:
Bruno Jouhier [MVP] <bj******@clu b-internet.fr> wrote:
You are right. The reference is passed by value, which is different from
passing by reference. I went back to my reference book on the subject (The
study of programming language -- Ryan Stansifer) and it confirms what you
say and what you write in your HTML document (and what the .NET
documentati on says).

But rather than say, "no" when people says that objects are passed by
reference, I think that you should say "almost" or "not quite", because they
don't always grasp the difference between "passed by reference" and
"reference passed by value", and then, they get confused.

To be fair, I *always* now give the URL to the more detailed
explanation. I'm trying to make the point that it really is quite a
sharp distinction in theoretical terms, even if a lot of the time they
end up having similar effects.

The most important point, as far as objects are concerned, is that the
reference is being passed around, and that the objects are never copied. So,
people don't need to use the "ref" keyword most of the time when they pass
an object because the reference is passed anyway (by value). The "ref"
keyword is only useful if they want the method to "return" a result (another
reference) through the parameter, which is not the usual case.

Indeed. And the fact that people *think* that they want to pass things
by reference when actually they want to pass a reference by value shows
how close things are in people's minds. By posting "almost" or "not
quite" there's an implication (IMO) that they really *are* quite close
together, whereas I'm trying to stress how different they are.

I'll think about it a bit more though, and maybe a bit less harsh :)


Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
19162
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
58
10181
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);
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK" by Dev C++. Here is:(I want to exchange the value of "a" and "b" with function "swap") void swap(int *pa, int *pb) {
9
1970
by: Alvin Bruney | last post by:
The more knowledgable I get about this .net world, the more questions I have. ..NET uses pass by reference for all objects....uhhh I mean pass by value. (Couldn't resist this jab) Consider a string object whose value is passed as a parameter to a function. The reference is pointing back to memory allocated on the heap where the contents of the string lives. Inside the function, I adjust the value, say string += "value", which means on...
8
2118
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects, each requiring a call to that method to fulfill their purpose. There could be 200, there could be more than 1000. That is a lot of references passed around. It feels heavy. Let us say i changed the signature of the interface method to:
11
8131
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
12
2687
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
7
10395
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
7
3307
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
9622
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10270
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10109
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
10051
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
9916
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...
1
7464
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
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.