473,748 Members | 10,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment operator for __value types

Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y );".

The op_Assign operator seems impossibly broken since it takes __value copies
of the two objects. Perhaps there is some other way.
Nov 22 '05 #1
20 1420
There is no way to overload the assign operator.

You may be able to get what you want by overloading a conversion operator to
convert from y to x; that would allow you to write "x = y";

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use
internally my own = operator for some of my value types, so I can say "x =
y;" rather than "x.Assign(y );".

The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is some other way.

Nov 22 '05 #2
Eric Gunnerson [MS] wrote:
There is no way to overload the assign operator.
That is unfortunately what I thought.

You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";
No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this behavior to
supply one's own functionality instead of the default behavior for
user-defined __value types. I admit I am not sure what the default behavior
is, whether it is a byte-by-byte copy of data from x to y, or whether it is
a field-by-field copy of data from x to y, or whether it takes into account
property values and gets and sets the appropriate properties when moving the
data. I know that when X is a __value class with properties, I want the get_
and set_ method for each property to be called if I say y = x, but I suspect
that is not the case. That is the reason I was hoping to override the
default assignment somehow.

My workaround is to provide a 'void X::Assign(X)' in order to do the
assignment, so that instead of writing y = x, I would write instead
y.Assign(x), but this is much less natural than the normal assignment
operator and y = x.

Needless to say, in C++ one can overload the default assignment operator
with 'X & operator = ( X /* or const X & */);'. Maybe CLR should revisit
this inability and make changes in the future which allows overriding the
assignment operator for user-devined __value classes possible.


This posting is provided "AS IS" with no warranties, and confers no
rights. "Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Is there a way to override the default processing of the assignment
operator for one's own __value types ? I realize I can program my
own Assign method, and provide that for end-users of my class, but I
would like to use internally my own = operator for some of my value
types, so I can say "x = y;" rather than "x.Assign(y );".

The op_Assign operator seems impossibly broken since it takes
__value copies of the two objects. Perhaps there is some other way.

Nov 22 '05 #3

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:ei******** ******@tk2msftn gp13.phx.gbl...
Eric Gunnerson [MS] wrote:
There is no way to overload the assign operator.
That is unfortunately what I thought.

You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";


No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this behavior

to supply one's own functionality instead of the default behavior for
user-defined __value types. I admit I am not sure what the default behavior is, whether it is a byte-by-byte copy of data from x to y, or whether it is a field-by-field copy of data from x to y, or whether it takes into account property values and gets and sets the appropriate properties when moving the data. I know that when X is a __value class with properties, I want the get_ and set_ method for each property to be called if I say y = x, but I suspect that is not the case. That is the reason I was hoping to override the
default assignment somehow.

My workaround is to provide a 'void X::Assign(X)' in order to do the
assignment, so that instead of writing y = x, I would write instead
y.Assign(x), but this is much less natural than the normal assignment
operator and y = x.

Needless to say, in C++ one can overload the default assignment operator
with 'X & operator = ( X /* or const X & */);'. Maybe CLR should revisit
this inability and make changes in the future which allows overriding the
assignment operator for user-devined __value classes possible.
Interesting...t his is one of those features(operat or overloading in general,
outside of mathematics of course) I think should be left to die a slow
death.


This posting is provided "AS IS" with no warranties, and confers no
rights. "Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Is there a way to override the default processing of the assignment
operator for one's own __value types ? I realize I can program my
own Assign method, and provide that for end-users of my class, but I
would like to use internally my own = operator for some of my value
types, so I can say "x = y;" rather than "x.Assign(y );".

The op_Assign operator seems impossibly broken since it takes
__value copies of the two objects. Perhaps there is some other way.


Nov 22 '05 #4
Edward Diener <ed******@tropi csoft.com> wrote:
You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";


No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this behavior to
supply one's own functionality instead of the default behavior for
user-defined __value types.


I have to say that I couldn't disagree more. One thing I *like* is
knowing what will happen on assignment (when only one type is involved,
and thus no implicit conversions) *without* having to look up the
details of the type.

If you want a way of creating a copy of a value, write a Copy method or
something similar (which I think is clearer than calling Assign on an
existing value) - but leave the rest of us with a system where we don't
need to wonder what every single line of code means, however simple!

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #5
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:ei******** ******@tk2msftn gp13.phx.gbl...
Eric Gunnerson [MS] wrote:
There is no way to overload the assign operator.
That is unfortunately what I thought.

You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";


No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this
behavior to supply one's own functionality instead of the default
behavior for user-defined __value types. I admit I am not sure what
the default behavior is, whether it is a byte-by-byte copy of data
from x to y, or whether it is a field-by-field copy of data from x
to y, or whether it takes into account property values and gets and
sets the appropriate properties when moving the data. I know that
when X is a __value class with properties, I want the get_ and set_
method for each property to be called if I say y = x, but I suspect
that is not the case. That is the reason I was hoping to override
the default assignment somehow.

My workaround is to provide a 'void X::Assign(X)' in order to do the
assignment, so that instead of writing y = x, I would write instead
y.Assign(x), but this is much less natural than the normal assignment
operator and y = x.

Needless to say, in C++ one can overload the default assignment
operator with 'X & operator = ( X /* or const X & */);'. Maybe CLR
should revisit this inability and make changes in the future which
allows overriding the assignment operator for user-devined __value
classes possible.


Interesting...t his is one of those features(operat or overloading in
general, outside of mathematics of course) I think should be left to
die a slow death.


It is alive and well, fortunately, but I am not going to get into a polemic
about it. I know only too well the arguments on both sides ( C++ has had it
from its inception ) and I know how much I approve of it myself and that is
all I will say.

Clearly .NET supports it although it is not CLS compliant. I just wanted to
use it in my own code for clarity, while providing a CLS Assign member
function also for other .NET languages.

Rather than get into an argument about overriding the built-in = operator
for value types, I will present a situation where it may not do what I want
it to do. I have a value type, let us call it X as in the above example,
which has a property, and also an event which is triggered when the property
changes. So in my property set function, I trigger the event. If I assign
one X to another using the built-in = operator, does my property set
function get called or does the built-in = operator just copy the underlying
X data from one to the other ? If the latter, then my event never gets
triggered, and event handlers which need to know when the property changes
never get called when using = to assign one object of the value type to
another.

In the Assign member function of value type X, I assign the property from
one to another which assures that the event gets triggered. If I could
override the built-in assignment operator = , I would do the same. Obviously
I can use Assign instead, but I have to remember to use it rather than the
more natural = assignment.


This posting is provided "AS IS" with no warranties, and confers no
rights. "Edward Diener" <ed******@tropi csoft.com> wrote in message
news:Ot******** ******@TK2MSFTN GP12.phx.gbl...
Is there a way to override the default processing of the assignment
operator for one's own __value types ? I realize I can program my
own Assign method, and provide that for end-users of my class, but
I would like to use internally my own = operator for some of my
value types, so I can say "x = y;" rather than "x.Assign(y );".

The op_Assign operator seems impossibly broken since it takes
__value copies of the two objects. Perhaps there is some other way.

Nov 22 '05 #6
Jon Skeet [C# MVP] wrote:
Edward Diener <ed******@tropi csoft.com> wrote:
You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";


No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this
behavior to supply one's own functionality instead of the default
behavior for user-defined __value types.


I have to say that I couldn't disagree more. One thing I *like* is
knowing what will happen on assignment (when only one type is
involved, and thus no implicit conversions) *without* having to look
up the details of the type.

If you want a way of creating a copy of a value, write a Copy method
or something similar (which I think is clearer than calling Assign on
an existing value) - but leave the rest of us with a system where we
don't need to wonder what every single line of code means, however
simple!


Do you feel the same thing about overriding other operators also, or is your
uncomfortablene ss just about overriding the assignment operator ? I am just
curious because it is fruitless to get into an argument about operator
overriding. Obviously most C++ programmers like me love it, since it has
been part of the language since its inception and we are very much aware of
its advantages and disadvantages, while many others who have never used it
dislike it, largely for the reason you have given.
Nov 22 '05 #7
Edward Diener <ed******@tropi csoft.com> wrote:
I have to say that I couldn't disagree more. One thing I *like* is
knowing what will happen on assignment (when only one type is
involved, and thus no implicit conversions) *without* having to look
up the details of the type.

If you want a way of creating a copy of a value, write a Copy method
or something similar (which I think is clearer than calling Assign on
an existing value) - but leave the rest of us with a system where we
don't need to wonder what every single line of code means, however
simple!


Do you feel the same thing about overriding other operators also, or is your
uncomfortablene ss just about overriding the assignment operator ? I am just
curious because it is fruitless to get into an argument about operator
overriding. Obviously most C++ programmers like me love it, since it has
been part of the language since its inception and we are very much aware of
its advantages and disadvantages, while many others who have never used it
dislike it, largely for the reason you have given.


I feel wary about introducing any operator overloads. They should be:

a) Entirely natural
b) The kind of thing which is bound to be commonly used
c) Very explicitly documented

For instance, it's handy having == and + overridden for string, and
having DateTime's relationship with TimeSpan (e.g. DateTime-
DateTime=TimeSp an, DateTime+TimeSp an=DateTime etc). For a few standard
types, it's fine - because everyone knows about it. When it comes to
3rd party classes which other developers will be less familiar with, I
get much more dubious.

I'm pretty uncomfortable with explicit conversion operators, and very
uncomfortable with implicit conversion operators. It basically means
code is being called where it doesn't look like any is. (Properties are
relatively obvious by convention - otherwise I'd be doubtful about
them, too.)

I can see the advantages when you know what's going on - but I'm a
*very* big fan of making the code as readable as possible to someone
who may come to maintain my code without necessarily being as
intimately familiar with the rest of the codebase as I am.

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

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:ey******** *****@TK2MSFTNG P11.phx.gbl...
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:ei******** ******@tk2msftn gp13.phx.gbl...
Eric Gunnerson [MS] wrote:
There is no way to overload the assign operator.

That is unfortunately what I thought.
You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";

No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this
behavior to supply one's own functionality instead of the default
behavior for user-defined __value types. I admit I am not sure what
the default behavior is, whether it is a byte-by-byte copy of data
from x to y, or whether it is a field-by-field copy of data from x
to y, or whether it takes into account property values and gets and
sets the appropriate properties when moving the data. I know that
when X is a __value class with properties, I want the get_ and set_
method for each property to be called if I say y = x, but I suspect
that is not the case. That is the reason I was hoping to override
the default assignment somehow.

My workaround is to provide a 'void X::Assign(X)' in order to do the
assignment, so that instead of writing y = x, I would write instead
y.Assign(x), but this is much less natural than the normal assignment
operator and y = x.

Needless to say, in C++ one can overload the default assignment
operator with 'X & operator = ( X /* or const X & */);'. Maybe CLR
should revisit this inability and make changes in the future which
allows overriding the assignment operator for user-devined __value
classes possible.
Interesting...t his is one of those features(operat or overloading in
general, outside of mathematics of course) I think should be left to
die a slow death.


It is alive and well, fortunately, but I am not going to get into a

polemic about it. I know only too well the arguments on both sides ( C++ has had it from its inception ) and I know how much I approve of it myself and that is all I will say.

Clearly .NET supports it although it is not CLS compliant. I just wanted to use it in my own code for clarity, while providing a CLS Assign member
function also for other .NET languages.

Rather than get into an argument about overriding the built-in = operator
for value types, I will present a situation where it may not do what I want it to do. I have a value type, let us call it X as in the above example,
which has a property, and also an event which is triggered when the property changes. So in my property set function, I trigger the event. If I assign
one X to another using the built-in = operator, does my property set
function get called or does the built-in = operator just copy the underlying X data from one to the other ? If the latter, then my event never gets
triggered, and event handlers which need to know when the property changes
never get called when using = to assign one object of the value type to
another.

In the Assign member function of value type X, I assign the property from
one to another which assures that the event gets triggered. If I could
override the built-in assignment operator = , I would do the same. Obviously I can use Assign instead, but I have to remember to use it rather than the
more natural = assignment.

The question here, in my mind anyway, is should that event be raised anyway?
Is x = y still x? or is it y with no handler? Thats part of what I am
worried about, an assignment that instead of performing proper value
semantic copying is used to copy only pieces of the value, breaking value
semantics. In my opinion when I set x to y, then any event handler
references recorded in x should be destroyed along with the other data in x,
and y should be put in its place. No event should be raised because the
property *didn't* change, the variable that originally contained the
property did, but that is an entirely different beast.
Nov 22 '05 #9

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:eV******** *****@TK2MSFTNG P11.phx.gbl...
Jon Skeet [C# MVP] wrote:
Edward Diener <ed******@tropi csoft.com> wrote:
You may be able to get what you want by overloading a conversion
operator to convert from y to x; that would allow you to write "x =
y";

No I want to overload the default behavior when one says:

X x;
X y;
// Do something with x to change it.
y = x;

where X is one of my own __value classes.

I think it is a weakness of the CLR that one can't override this
behavior to supply one's own functionality instead of the default
behavior for user-defined __value types.
I have to say that I couldn't disagree more. One thing I *like* is
knowing what will happen on assignment (when only one type is
involved, and thus no implicit conversions) *without* having to look
up the details of the type.

If you want a way of creating a copy of a value, write a Copy method
or something similar (which I think is clearer than calling Assign on
an existing value) - but leave the rest of us with a system where we
don't need to wonder what every single line of code means, however
simple!


Do you feel the same thing about overriding other operators also, or is

your uncomfortablene ss just about overriding the assignment operator ? I am just curious because it is fruitless to get into an argument about operator
overriding. Obviously most C++ programmers like me love it, since it has
been part of the language since its inception and we are very much aware of its advantages and disadvantages, while many others who have never used it
dislike it, largely for the reason you have given.

Since I meant to clarify a bit in the other thread but forgot to before I
posted, I'll post this bit here.

I personally have nothing against responsible use of operator overloading,
like Jon I like being able to overload == when appropriate, and having
string +string and other such short cuts is handy, however some constructs
are fincky and unpleasent(and considering the .NET implementation of
operator overloading, some things are a little t0o fussy anyway). The
feature is powerful, but dangerous, and I frown more upon unfettered and
irresponsible use of the feature than the feature itself. I see far too
often, even in C#, where an operator overload is done simply because the
original programer would rather type + or because he doesn't want to
explicitly cast, that kind of behaviour is troubling.
And, while those experianced in C++ will be discriminating( although not
always to my tastes), its not the masters I worry about, they, unfortunate
as it may be, are the minority. It is the mediocre and the amateur
programmer that will usually cause real damage with operator overloading.

Now, that said, I do have a particular problem with overloading the
assignment operator, as I addressed in part in my other posts. I simply
don't like to be guessing what I'm actually doing in the simplist line of
code possible in the language I'm working in. I don't think I'd like to find
a ref type that is always actually a value assignement, nor do I want to
find value assignments that aren't really value assignments, but partial
copies into the existing valuetype. Its just to complicated and it is far
too easy to get caught in a surprise because of it.
Nov 22 '05 #10

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

Similar topics

16
2616
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
8
1807
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value class is created, I dynamically allocate an object of the class with the legacy C++ code. However because the __value class has no destructor, I can never release that allocated memory. Why does a __value class allow no destructor ? Without it I...
0
914
by: Marcus Kwok | last post by:
I have been reading through the ManagedExtensionsSpec.doc file and I thought something was not clear. If I have a __value class (really a __value struct but that shouldn't make a difference) and I want a non-variable-size array of them, are there any advantages/disadvantages to using a __gc array versus a __nogc array? Section 4.5.3 (__gc and __nogc Keywords and Arrays) says:
0
8989
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
8828
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
9367
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
9319
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
8241
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...
0
4599
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...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
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
2780
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.