469,267 Members | 1,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

How to override the operator =


Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #1
11 21272
I am sorry that you can't override operator= in C#.
"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #2
You can override the implicit cast - operator. (see keyword implicit in
MSDN)

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #3
Could you adjust your clock?

José
"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #4
Simon Trew <ten.egnaro@werts> wrote:
If the poster is in China (.cn address) the clock may be correct.


Then their timezone isn't.

The original post had a date header of:

Date: Tue, 19 Aug 2003 11:15:18 -0700

That can't possibly be right, as that time (18:15 GMT) hasn't yet
happened.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
Code in C++ :-) or just override equals in C#
Ashish

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:el**************@tk2msftngp13.phx.gbl...

Hi All :
How to override the class operator = like C++ ?

Best regards !

Nov 15 '05 #6
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish


Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be another
solution.

Nathan
Nov 15 '05 #7
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish
Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another solution.

Nathan

Nov 15 '05 #8
I apologize I mean override not overload....its been a long week :)

"Chris Blanco" <cb***********@necam.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
I don't think you can override assignment operators. Look into overloading
the MemberwiseClone() function if you need capability beyond what it gives
you.

Using your example it would be:
MyClass A = new MyClass();
MyClass B = new MyClass();
....
B=(MyClass)A.MemberwiseClone();

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
> Code in C++ :-) or just override equals in C#
> Ashish

Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan



Nov 15 '05 #9
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
Code in C++ :-) or just override equals in C#
Ashish


Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan


Nov 15 '05 #10
OK ,Thanks a lot .
"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:O2*******************@twister.austin.rr.com.. .
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
"Ashish" <ab*@hotmail.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
> Code in C++ :-) or just override equals in C#
> Ashish

Wouldn't that override "==", not "="?

Can I ask why you would possibly want to override "="? There must be

another
solution.

Nathan



Nov 15 '05 #11
That my my answer 2 days before ;-)

GP

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
OK ,Thanks a lot .
"Chad Myers" <cm****@N0.SP.4M.austin.rr.com> wrote in message
news:O2*******************@twister.austin.rr.com.. .
You cannot override/overload or otherwise modify
the assignment (=) or new operators in .NET.

There is specific logic that .NET uses for assignment
that is required for the memory management and
garbage collection.

I think what you may want is to override the
implicit and explicit cast operator.

For example:

MyFancyNumberClass mfnc = new MyFancyNumberClass():

// implicit cast
int foo = mfnc;

If you wrote MyFancyNumberClass and you wanted
people to copy it to an int, you'd use the
implicit cast operator to convert mfnc to an
integer.

You can also override explicit casts:
int foo = (int) mfnc;

-c

"apex" <ap*****@sjtu.edu.cn> wrote in message
news:O%****************@TK2MSFTNGP10.phx.gbl...
For example:
MyClass A = new MyClass();
MyClass B = new MyClass();
A.Item1 = ...;
A.Item2 = ...;
B = A;

"Treefrog" <tr******@mountainbikes.co.uk> wrote in message
news:3f********@news.1anetworks.net...
> "Ashish" <ab*@hotmail.com> wrote in message
> news:eR**************@tk2msftngp13.phx.gbl...
> > Code in C++ :-) or just override equals in C#
> > Ashish
>
> Wouldn't that override "==", not "="?
>
> Can I ask why you would possibly want to override "="? There must be
another
> solution.
>
> Nathan
>
>



Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mathieu Malaterre | last post: by
3 posts views Thread by James Aguilar | last post: by
2 posts views Thread by Shark | last post: by
5 posts views Thread by meng.frank | last post: by
52 posts views Thread by Jim Langston | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.