473,387 Members | 1,463 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,387 software developers and data experts.

Problem about operator = in C#

I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override
in c#.Thank you
Nov 15 '05 #1
17 1387
"Archer" <xy*******@etang.com> wrote in
news:Ow**************@tk2msftngp13.phx.gbl:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be
override in c#.Thank you


What about using System.Collections.ArrayList instead of constructing your
own List?

And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at
Nov 15 '05 #2
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
"Peter Koen" <koen-newsreply&snusnu.at> дÈëÓʼþ
news:ez*************@TK2MSFTNGP12.phx.gbl...
"Archer" <xy*******@etang.com> wrote in
news:Ow**************@tk2msftngp13.phx.gbl:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be
override in c#.Thank you


What about using System.Collections.ArrayList instead of constructing your
own List?

And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at

Nov 15 '05 #3
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
"Peter Koen" <koen-newsreply&snusnu.at> дÈëÓʼþ
news:ez*************@TK2MSFTNGP12.phx.gbl...
"Archer" <xy*******@etang.com> wrote in
news:Ow**************@tk2msftngp13.phx.gbl:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be
override in c#.Thank you


What about using System.Collections.ArrayList instead of constructing your
own List?

And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at

Nov 15 '05 #4
"Archer" <xy*******@etang.com> wrote in
news:#T**************@TK2MSFTNGP12.phx.gbl:
What about the object i created for e?
is it be released auto?And what is the difference between stuct &
class in C#?


All objects will be released and collected by the garbage collector.

structs are value types
classes are reference types

when you're asking such questionis it would be good if you read some basic
tutorial before you start thinking about stuff like linked lists.

http://msdn.microsoft.com/netframewo...d/default.aspx

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at
Nov 15 '05 #5
In article <Ow**************@tk2msftngp13.phx.gbl>, Archer wrote:

Let's look at the code step by step...
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List(); // d is a reference to a new List object, we'll call the object List #1 List e=new List(); // e is a reference to a new List object, let's call the object List #2 e.Data=4; // The value of Data in List #2 = 5 d=e; // d is now a reference to the List #2 object (just like 'e'). The
// List #1 object is not referenced, and may now be garbage collected d.Data=5; // The value of Data in List #2 = 5
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do?


Don't change it? :)

It's hard to know exactly what you're after here, so it's hard to
suggest an alternative. But the other poster had a good point in
suggesting walking through a good tutorial. Object references are at
the heart of C#, and it's a good idea to have a very solid handle on
them.
--
David
dfoster at
hotpop dot com
Nov 15 '05 #6
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

| after the programme below ran,
| List d=new List();
d --> ListA

| List e=new List();
e --> ListB

| e.Data=4;
ListB.Data becomes 4

| d=e;
d --> ListB
e --> ListB
That is, both refer to List B now, so..

| d.Data=5;
ListB.Data becomes 5

| if I don't want the
| e.Data'value changed,what should i do?

Well first you have to ask yourself what you want to achieve with that
statement (d = e) ?

| btw:the operator = can't be override in c#.

Yeah, and you shouldn't :)

RD
Archer wrote:

| I defined my class as:
| class List
| {
| public List Next;
| public int Data;
| public List()
| {
| Data=0;
| }
| }
| ¡*¡*¡*¡*¡*¡*¡*¡*
| after the programme below ran,
| List d=new List();
| List e=new List();
| e.Data=4;
| d=e;
| d.Data=5;
| The value of e.Data became 5,What does it do?if I don't want the
| e.Data'value changed,what should i do? btw:the operator = can't be
override
| in c#.Thank you
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQE/m1eLwEwccQ4rWPgRAjtRAJwMluP4Pch7YTvtC9IZQxKvnp0jJQ CfYLh9
VsNiSA8w9EEWh0oL/Zdq3+w=
=HzXr
-----END PGP SIGNATURE-----

Nov 15 '05 #7
<Peter Koen <koen-newsreply&snusnu.at>> wrote:
And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.


No, nothing is passed by reference unless you say so. There's a
difference between pass-by-reference semantics and references being
passed by value.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
Archer <xy*******@etang.com> wrote:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override
in c#.Thank you


If you want d and e to be references to independent lists, you need to
clone the list rather than having two references to the same object.

As other posters have said, it's vital to understand how reference
types and value types work. There's some information on this in

http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #9
Jon, please, could you give me a better explanation of the sentence below?

"Instance variables for a value type are stored in the same context as the
variable that declares the value type.
The memory slot for the instance effectively contains the slots for each
field within the instance. That means (...) that a struct variable declared
within a method will always be on the stack, whereas a struct variable which
is an instance field of a class will be on the heap"
It got a little bit confused for me. Thanks for your time.
--

---------------
Valmir Cinquini
Italian Citizenship: visit http://www.cinquini.com.br
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Archer <xy*******@etang.com> wrote:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be override in c#.Thank you


If you want d and e to be references to independent lists, you need to
clone the list rather than having two references to the same object.

As other posters have said, it's vital to understand how reference
types and value types work. There's some information on this in

http://www.pobox.com/~skeet/csharp/parameters.html
and
http://www.pobox.com/~skeet/csharp/memory.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
Hi,
'struct' is value type and 'class' is reference type.
Thanks And Regards.
Madhanmohan S
"Archer" <xy*******@etang.com> wrote in message
news:eJ*************@TK2MSFTNGP10.phx.gbl...
What about the object i created for e?
is it be released auto?And what is the difference between stuct & class in
C#?
"Peter Koen" <koen-newsreply&snusnu.at> дÈëÓʼþ
news:ez*************@TK2MSFTNGP12.phx.gbl...
"Archer" <xy*******@etang.com> wrote in
news:Ow**************@tk2msftngp13.phx.gbl:
I defined my class as:
class List
{
public List Next;
public int Data;
public List()
{
Data=0;
}
}
¡*¡*¡*¡*¡*¡*¡*¡*
after the programme below ran,
List d=new List();
List e=new List();
e.Data=4;
d=e;
d.Data=5;
The value of e.Data became 5,What does it do?if I don't want the
e.Data'value changed,what should i do? btw:the operator = can't be
override in c#.Thank you


What about using System.Collections.ArrayList instead of constructing your own List?

And if you really want your own impl: a class is always passed by
reference. so you will need a struct for your desired behaviour.

--
best regards

Peter Koen
-----------------------------------
MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS
http://www.kema.at


Nov 15 '05 #11
Bill McNeal <Bi********@Bol.com.br> wrote:
Jon, please, could you give me a better explanation of the sentence below?

"Instance variables for a value type are stored in the same context as the
variable that declares the value type.
The memory slot for the instance effectively contains the slots for each
field within the instance. That means (...) that a struct variable declared
within a method will always be on the stack, whereas a struct variable which
is an instance field of a class will be on the heap"
It got a little bit confused for me. Thanks for your time.


It means that if you have:

public class Foo
{
int x;

void Something()
{
int y = 5;
}
}

then the value of y is on the stack when Something is called, whereas
the value of x for any instance is stored with the rest of the
information about that instance, on the heap.

Does that help?

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

By the way, reading the articles you suggested, I'd like to answer one more
wuestion: What's the big deal using output parameters, if they behave like
reference parameters? Both need to have the variables assigned before their
use, and both change the variable's value in the calling code if the called
function change them.

So why use them?

Thanks again for your time.

--

---------------
Valmir Cinquini


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bill McNeal <Bi********@Bol.com.br> wrote:
Jon, please, could you give me a better explanation of the sentence below?
"Instance variables for a value type are stored in the same context as the variable that declares the value type.
The memory slot for the instance effectively contains the slots for each
field within the instance. That means (...) that a struct variable declared within a method will always be on the stack, whereas a struct variable which is an instance field of a class will be on the heap"
It got a little bit confused for me. Thanks for your time.


It means that if you have:

public class Foo
{
int x;

void Something()
{
int y = 5;
}
}

then the value of y is on the stack when Something is called, whereas
the value of x for any instance is stored with the rest of the
information about that instance, on the heap.

Does that help?

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

Nov 15 '05 #13
Bill McNeal <Bi********@Bol.com.br> wrote:
By the way, reading the articles you suggested, I'd like to answer one more
wuestion: What's the big deal using output parameters, if they behave like
reference parameters? Both need to have the variables assigned before their
use
No they don't - out parameters don't need to have the variables
assigned before their use (in the calling code) and they *do* need to
have values assigned before the method completes normally (ie without
an exception). The method can't read the value of the variable until it
assigns it, either.

My article (http://www.pobox.com/~skeet/csharp/parameters.html) gives a
little bit more detail on this, and an example - I suggest you play
around with the example code, changing out to ref, commenting out
assignments etc, and see what the differences are.
and both change the variable's value in the calling code if the called
function change them.


Yes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
Ok Jon, thanks again for your reply. I have already played a little with the
source code provided by your article, and i understood all about outpu
parameters (it was easier than where ref and value parameters differs)

I just couldnt figure out which benefits I can get using output parameters
instead of reference parameters. Just when I dont have a value to assign to
a variable before pass it to a function? So what? I can assign some "dummy"
value and pass it to a function as a ref parameter.

Please, if there is something I dont know, them let me know.

Thanks again.

Valmir
Nov 15 '05 #15
Bill McNeal <Bi********@Bol.com.br> wrote:
Ok Jon, thanks again for your reply. I have already played a little with the
source code provided by your article, and i understood all about outpu
parameters (it was easier than where ref and value parameters differs)

I just couldnt figure out which benefits I can get using output parameters
instead of reference parameters. Just when I dont have a value to assign to
a variable before pass it to a function? So what? I can assign some "dummy"
value and pass it to a function as a ref parameter.

Please, if there is something I dont know, them let me know.


Well, a few things:

o Assigning a dummy parameter value is ugly - unless you specifically
document that you don't expect the value to get used, the maintainer
of the code may well try to work out why you've used that particular
value, and what its significance is.

o The fact that it's an out parameter means that the caller knows that
the value of the variable really doesn't matter at all, and enforces
that for the called method (by treating it as unassigned to start
with).

o The fact that it's an out parameter forces the called method to
assign a value to it before leaving the method, which is as helpful
as the compiler error message of "not all code paths return a
value" - by declaring the parameter as "out" you're basically saying
that you know you definitely want to set the value before you return,
and the compiler will help to make sure that you do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #16
Well, thanks again for your good explanation. IMHO I never faced a situation
where I needed an output parameters. But the second and third points you
describe really gave me some good reasons to use output parameters.

Despite the dummy value turns the code ugly....well, usually we finished
being "parents"of an application for the rest of our lives while working for
a company. Unless you work for a very organized company, once you dont work
for them anymore, other person will try understand what you code and
probabilly, will change to their "code style".
Well, thanks again for you patience replying me.

--

---------------
Valmir Cinquini

Cidadania Italiana: visite http://www.cinquini.com.br
Banda larga de 1º mundo: Visite http://speedy.bravehost.com/speedy.htm
Lista MS-SQL: http://br.groups.yahoo.com/group/mssql-pt/join
Lista C#: http://br.groups.yahoo.com/group/csharp-pt/join
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bill McNeal <Bi********@Bol.com.br> wrote:
Ok Jon, thanks again for your reply. I have already played a little with the source code provided by your article, and i understood all about outpu
parameters (it was easier than where ref and value parameters differs)

I just couldnt figure out which benefits I can get using output parameters instead of reference parameters. Just when I dont have a value to assign to a variable before pass it to a function? So what? I can assign some "dummy" value and pass it to a function as a ref parameter.

Please, if there is something I dont know, them let me know.


Well, a few things:

o Assigning a dummy parameter value is ugly - unless you specifically
document that you don't expect the value to get used, the maintainer
of the code may well try to work out why you've used that particular
value, and what its significance is.

o The fact that it's an out parameter means that the caller knows that
the value of the variable really doesn't matter at all, and enforces
that for the called method (by treating it as unassigned to start
with).

o The fact that it's an out parameter forces the called method to
assign a value to it before leaving the method, which is as helpful
as the compiler error message of "not all code paths return a
value" - by declaring the parameter as "out" you're basically saying
that you know you definitely want to set the value before you return,
and the compiler will help to make sure that you do.

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

Nov 15 '05 #17
Bill McNeal <Bi********@Bol.com.br> wrote:
Well, thanks again for your good explanation. IMHO I never faced a situation
where I needed an output parameters.
Oh absolutely - I very, very rarely use either output *or* ref
parameters. I think they're generally a bad sign, and whenever I
consider using them, I think about whether a restructuring would
actually be better.
But the second and third points you
describe really gave me some good reasons to use output parameters.
Goodo :)
Despite the dummy value turns the code ugly....well, usually we finished
being "parents"of an application for the rest of our lives while working for
a company. Unless you work for a very organized company, once you dont work
for them anymore, other person will try understand what you code and
probabilly, will change to their "code style".
Unless you're the only programmer in the company, that seems unlikely
to me. I often need to look at code written by other members of the
company, especially when the product isn't developed by a single
person. I guess different companies work in different ways though. I
still don't like the idea of assigning values which will never be read
though :)
Well, thanks again for you patience replying me.


No problem :)

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

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

Similar topics

7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
4
by: Andre Paim Lemos | last post by:
Hi, I'm having some compiler problems when I try to use make_heap(), push_heap() and pop_heap(). I am compiling my code on gcc version 3.3.1 (SuSE Linux). I am using the heap related methods to...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: Tony Johansson | last post by:
Hello Experts! I have the user defined class called Boolean that should be handle all kind of logic expression such as if (a && b && c) or if (!a && b && c) osv I have the class definition...
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
6
by: junw2000 | last post by:
Hi, I wrote a simple code about operator overloading. But it can not compile. Below is the code: #include <iostream> using namespace std;
4
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
2
by: Dark Wind | last post by:
Hi, I have been using OPT++ to solve a non linear programming problem. I am totally new to C++, but I looked at an example given on OPT++ website and modified it according to my problem. But I...
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:
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...
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
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...
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...

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.