Re: Overloading binary + operator
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Tue, 15 Apr 2008 07:03:27 -0700 (PDT)
On Apr 15, 3:58 am, Sapo19875 <samuele.mo...@xxxxxxxxxxx> wrote:
<snip>
public override operator +(MyClass class1, MyClass class2)
{
MyClass class3 = class1;
class3.property1 = class1.property1 + class2.property2;
.......
return class3;
}
You're changing the original value here (assuming it's genuinely a
class). You should be creating a *new* instance of MyClass, not just
changing the existing instance. The line:
MyClass class3 = class1;
should be something to create a new instance of MyClass from class1,
e.g.
MyClass class3 = new MyClass(class1);
where you define the appropriate constructor.
If that doesn't help, please post a short but complete program
demonstrating the problem. This clearly isn't your real code, and we
can't tell which mistakes (e.g. using property2 instead of property1)
are in your real code and which aren't. A complete program which we
can compile and run will be easier to fix.
Jon
.
- References:
- Overloading binary + operator
- From: Sapo19875
- Overloading binary + operator
- Prev by Date: Re: How to extract all exceptions for any method
- Next by Date: Re: omit blank lines in file using StreamReader
- Previous by thread: Re: Overloading binary + operator
- Next by thread: C# add event handler to DataGridViewComboBoxCell
- Index(es):
Relevant Pages
|