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

operator+=

Is there any way to define the above operator for my own class? I seem to be
able to define every single one, except the one I want to (always the way :)

The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented. Is this
done via operator+?

I am using C# 2.0

--
Regards, Fil.
Jun 5 '06 #1
10 3191
Fil Mackay wrote:
Is there any way to define the above operator for my own class? I
seem to be able to define every single one, except the one I want to
(always the way :)

The main reason I ask is that the event interfaces use the "Event +=
new EventHandler(..)" syntax - I am not sure how this gets
implemented. Is this done via operator+?

I am using C# 2.0


Yes it's the + operator. :)

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jun 5 '06 #2
Yes, of course - but the operator+= gives you additional capabilities to
improve efficiency, by modifying the item inline, rather than creating a new
instance

Not sure if you're aware, but C++ has an operator+=; you dont need to hack
it with operator+.

Regards, Fil.

------------------------
"Frans Bouma [C# MVP]" wrote:
Fil Mackay wrote:
Is there any way to define the above operator for my own class? I
seem to be able to define every single one, except the one I want to
(always the way :)

The main reason I ask is that the event interfaces use the "Event +=
new EventHandler(..)" syntax - I am not sure how this gets
implemented. Is this done via operator+?

I am using C# 2.0


Yes it's the + operator. :)

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Jun 5 '06 #3
Fil,

1. In c# you overload += by overloading the + operator.
2. += for events is handled by the C# compiler and doesn't involve operator
overloading. Some may argue with this statement, but what I mean is that
there is no 'op_*' method generated.
--

Stoitcho Goutsev (100)

"Fil Mackay" <Fi*******@discussions.microsoft.com> wrote in message
news:21**********************************@microsof t.com...
Is there any way to define the above operator for my own class? I seem to
be
able to define every single one, except the one I want to (always the way
:)

The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented. Is
this
done via operator+?

I am using C# 2.0

--
Regards, Fil.

Jun 5 '06 #4
> 1. In c# you overload += by overloading the + operator.

Unfortunately, I cannot do this - since the type of operation I am doing
modifies the class in-place, rather than creating a new instance.

I could dodge this and implement an in-place addition, but that would
produce unexpected results where the user actually did a b + c, instead of b
+= c
2. += for events is handled by the C# compiler and doesn't involve operator
overloading. Some may argue with this statement, but what I mean is that
there is no 'op_*' method generated.


Right - thought that might be the case.

Regards, Fil.

Jun 5 '06 #5
Why the fascination with the += operator in this case? Abusing it is (as you
yourself say) likely to cause grief - so why not just have a .Combine() or
..Append() method (peronally I think .Add() might be confusing as a name, as
you might expect this to be a non-consequential function that returns the
result).

With intellisense this doesn't really add an effort, and it keeps things
clean and simple. Plus you can specify such a method on an interface, which
you can't do with an operator.

Marc
Jun 5 '06 #6
KH
> The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented.
Fil, it sounds like you might be looking for the 'add' and 'remove' methods
for maintaining event lists. The syntax is similar to 'get' and 'set' with
properties. It's very poorly documented (the keywords don't even appear in
the C# keyword list)

You'll find examples in MSDN -- C# programming guide "How to: Implement
Interface Events"

- KH
"Fil Mackay" wrote:
Is there any way to define the above operator for my own class? I seem to be
able to define every single one, except the one I want to (always the way :)

The main reason I ask is that the event interfaces use the "Event += new
EventHandler(..)" syntax - I am not sure how this gets implemented. Is this
done via operator+?

I am using C# 2.0

--
Regards, Fil.

Jun 5 '06 #7
> Why the fascination with the += operator in this case? Abusing it is (as you
yourself say) likely to cause grief
+= is neat because that describes exactly what I want to do. Abusing
operator+ would be a bad thing, yes. But if I could get my hands on
operator+= I would definitely use it.

- so why not just have a .Combine() or ..Append() method (peronally I think .Add() might be confusing as a name, as
you might expect this to be a non-consequential function that returns the
result).


Yes, I will use .Add(other) - that seems to next most sensible/obvious
solution.

Thanks for the post!

Regards, Fil.

Jun 5 '06 #8
Fil Mackay wrote:
Yes, of course - but the operator+= gives you additional capabilities
to improve efficiency, by modifying the item inline, rather than
creating a new instance

Not sure if you're aware, but C++ has an operator+=; you dont need to
hack it with operator+.
Unfortunately, C# doesn't have the same operator voodoo power as C++
has, so you can only override a fixed, limited set of operators.

FB

Regards, Fil.

------------------------
"Frans Bouma [C# MVP]" wrote:
Fil Mackay wrote:
Is there any way to define the above operator for my own class? I
seem to be able to define every single one, except the one I want
to (always the way :)

The main reason I ask is that the event interfaces use the "Event
+= new EventHandler(..)" syntax - I am not sure how this gets
implemented. Is this done via operator+?

I am using C# 2.0


Yes it's the + operator. :)

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jun 6 '06 #9
> += is neat because that describes exactly what I want to do.
IMO (for what that matters) I disagree; "x += y;" is generally considered as
shorthand for "x = x + y;". This means reassigning the value of x; indeed,
this is what happens (via the compiler) for events and delegates :
Delegate.Combine is a static method that returns a new Delegate, so {event}
+= {handler} translates to (via the backing delegates and event pass-thrus):
{delegate backer} = Delegate.Combine({delegate backer}, {handler});

What you want to do is leave the original variable (in terms of references,
not contents) the same. To me, this is not "+="; this is an instance
operator.

Anyways, that's just my take ;-p

Marc
Jun 6 '06 #10
Fil,

Yes that is the difference in overloading these operators in c# and c++.
--

Stoitcho Goutsev (100)

"Fil Mackay" <Fi*******@discussions.microsoft.com> wrote in message
news:BC**********************************@microsof t.com...
1. In c# you overload += by overloading the + operator.


Unfortunately, I cannot do this - since the type of operation I am doing
modifies the class in-place, rather than creating a new instance.

I could dodge this and implement an in-place addition, but that would
produce unexpected results where the user actually did a b + c, instead of
b
+= c
2. += for events is handled by the C# compiler and doesn't involve
operator
overloading. Some may argue with this statement, but what I mean is that
there is no 'op_*' method generated.


Right - thought that might be the case.

Regards, Fil.

Jun 6 '06 #11

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
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...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.