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 13 17894
Here is how I would write it:
private int _count;
private Area _myArea;
public MyForm(int count, Area myArea) { _count = count; _myArea =
myArea; }
// Now, you can use _myArea in any method, like:
public void DoSomethingWithMyArea() { _myArea.DoSomething(); }
Note: You do not need the ref keyword in the constructor because objects are
always passed "by reference" in C#. So, with the code above (no ref
keyword), _myArea and myArea point to the same object, which is probably
what you want.
The ref keyword really means in/out. It means that the caller passes one
object and receives a different object when the call completes (so, you are
not passing a reference to the object, but rather a reference to a reference
(*) ). It is only useful when a method needs to return several results.
(*) This is a slight simplification of the real picture because in C# the
assignment of the new value is done by the caller rather than by the callee
(so that you can pass a property with a pair of r/w accessors), so if you
use the ref keyword, the value is copied to a temporary variable before the
call, the callee receives the address of the temporary variable, the callee
writes its new value to the temporary variable, and then the caller assigns
the temporary variable to the argument (which must be a valid target for
assignment) after the call completes.
Bruno.
"Abe Frohnman" <us******@SPAMexperimentzero.org> a écrit dans le message de
news:sM**********************@news.easynews.com... 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
Bruno Jouhier [MVP] <bj******@club-internet.fr> wrote: Note: You do not need the ref keyword in the constructor because objects are always passed "by reference" in C#.
No they're not. References are passed by value. I know it may be easier
to say this than to explain it fully, but I think it does harm in the
end.
To the OP: see http://www.pobox.com/~skeet/csharp/parameters.html for a
full discussion about what "passing by reference" really means, and
what C# does.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Hmm...so C# passes objects differently than C++ does? I know that if you
don't explicitly tell a C++ function/method that the object is going to
be passed in by reference, it gets passed in by value. So just to ensure
that I'm understanding this correctly, whenever I pass an object into a
function/method it'll >always< be passed in by reference? And any
changes I make to that object in the method will affect the object
outside of that method?
AF
Bruno Jouhier [MVP] wrote: Here is how I would write it:
private int _count; private Area _myArea;
public MyForm(int count, Area myArea) { _count = count; _myArea = myArea; }
// Now, you can use _myArea in any method, like: public void DoSomethingWithMyArea() { _myArea.DoSomething(); }
Note: You do not need the ref keyword in the constructor because objects are always passed "by reference" in C#. So, with the code above (no ref keyword), _myArea and myArea point to the same object, which is probably what you want.
The ref keyword really means in/out. It means that the caller passes one object and receives a different object when the call completes (so, you are not passing a reference to the object, but rather a reference to a reference (*) ). It is only useful when a method needs to return several results.
(*) This is a slight simplification of the real picture because in C# the assignment of the new value is done by the caller rather than by the callee (so that you can pass a property with a pair of r/w accessors), so if you use the ref keyword, the value is copied to a temporary variable before the call, the callee receives the address of the temporary variable, the callee writes its new value to the temporary variable, and then the caller assigns the temporary variable to the argument (which must be a valid target for assignment) after the call completes.
Abe Frohnman <us******@SPAMexperimentzero.org> wrote: Hmm...so C# passes objects differently than C++ does? I know that if you don't explicitly tell a C++ function/method that the object is going to be passed in by reference, it gets passed in by value. So just to ensure that I'm understanding this correctly, whenever I pass an object into a function/method it'll >always< be passed in by reference? And any changes I make to that object in the method will affect the object outside of that method?
You never pass a reference type object at all - you only pass a
reference, and that reference is passed by value. Any change to the
object referred to is also seen via the reference outside the method -
there's only one actual object involved.
See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Abe Frohnman <us******@SPAMexperimentzero.org> wrote: Right, right. Sorry I've not yet had my morning coffee. :) Let me rephrase that: In C#, objects are passed by reference automatically.
No, objects are never passed. References to objects are passed by
value. This is different to objects being passed by reference.
No need to use the 'ref' keyword in front of them?
That really depends on exactly what you want to do.
So if I have a class that takes a reference to an object in its constructor, what's the best way to use that reference later in the class?
ie: MyForm(myClass foo) {...}
should the constructor do something like: this.foo = foo; or is there a better way?
Assuming it has a foo field, then yes, that's perfectly okay.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
hehe, I suggest you have another cup of coffee :) You keep saying the
same thing...
lemme give it a try this time In C#, objects are passed by reference automatically. No need to use the 'ref' keyword in front of them?
Objects aren't passed.. objects are on the heap.. if you have something
like :
MyClass foo = new MyClass();
You instantiate an object which is then stored in the heap.. the
reference-type variable 'foo' is stored on the stack and holds the
reference to this object. when you pass this variable onto a method like:
MyForm f = new MyForm(MyClass foo2)
you actually pass the value of foo (which is the reference to the actual
object in the heap) to foo2.. foo2 is then stored on the stack with the
passed reference as its value which was 'copied' accross. Now, foo and
foo2 both have access to the heap-allocated object. If foo2 were to
change the object in some way, foo will be able to see the change -
however, since foo2 is another variable on the stack, making changes to
foo2 itself will not affect foo. Consider this:
MyForm(MyClass foo2) {
foo2 = new MyClass();
// foo2, above, now holds a reference to a newly allocated object
// foo, however, was not affected
}
-Andre
So if I have a class that takes a reference to an object in its constructor, what's the best way to use that reference later in the class?
ie: MyForm(myClass foo) {...}
should the constructor do something like: this.foo = foo; or is there a better way?
Thanks for the help!
Jon Skeet wrote:
You never pass a reference type object at all - you only pass a reference, and that reference is passed by value. Any change to the object referred to is also seen via the reference outside the method - there's only one actual object involved.
See http://www.pobox.com/~skeet/csharp/parameters.html for more information.
So what is the outcome of this?
Objects are always passed by reference or value?
They are passed by value.
-Andre
G wrote: So what is the outcome of this? Objects are always passed by reference or value?
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.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
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.com> a écrit dans le message de
news:MP************************@news.microsoft.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.com> http://www.pobox.com/~skeet/ If replying to the group, please do not mail me too
Bruno Jouhier [MVP] <bj******@club-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.com> http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
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.btinternet.com...
So what is the outcome of this? Objects are always passed by reference or value?
Ahh.. you guys already got to it :) I was hoping you would.
-Andre
Jon Skeet wrote: Bruno Jouhier [MVP] <bj******@club-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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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: 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...
|
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,...
|
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...
|
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);
|
by: AMP |
last post by:
Hello,
I have this in form1:
namespace Pass
{
public partial class Form1 : Form
{
public Form2 form2;
public Form1()
{
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |