Overloading binary + operator

S

Sapo19875

Hi all,
I need to sum two classes in this way

MyClass CT = myCollectionOfMyClass[0];
MyClass C1 = myCollectionOfMyClass[1];

CT = CT + C1;

In myClass exists the method to overload + operator and the method
looks like this:

public override operator +(MyClass class1, MyClass class2)
{
MyClass class3 = new MyClass();
class3.property1 = class1.property1 + class2.property2;
.......
return class3;
}

If I execute this code, the value of CT.property1 is 0. Also if
C1.property is 1.

To obtain the correct result (1) I must to change the code in this
way:

public override operator +(MyClass class1, MyClass class2)
{
MyClass class3 = class1;
class3.property1 = class1.property1 + class2.property2;
.......
return class3;
}

It seems that when I create a new istance of MyClass in the overloaded
method, the CT class was cleared.

Anyone knows the correct way to do this????

Bye
Sapo
 
H

Hans Kesting

It happens that Sapo19875 formulated :
Hi all,
I need to sum two classes in this way

MyClass CT = myCollectionOfMyClass[0];
MyClass C1 = myCollectionOfMyClass[1];

CT = CT + C1;

In myClass exists the method to overload + operator and the method
looks like this:

public override operator +(MyClass class1, MyClass class2)
{
MyClass class3 = new MyClass();
class3.property1 = class1.property1 + class2.property2;
.......
return class3;
}

If I execute this code, the value of CT.property1 is 0. Also if
C1.property is 1.

Bye
Sapo

How are you checking those results? The variable CT should contain a
new reference to that (new) "class3" instance. But the value of
myCollectionOfMyClass[0] did not change, that still points to the "old"
CT.

Hans Kesting
 
S

Sapo19875

How are you checking those results? The variable CT should contain a
new reference to that (new) "class3" instance. But the value of
myCollectionOfMyClass[0] did not change, that still points to the "old"
CT.

Hans Kesting

thanks Hans for your quickly reply.
the problem is right there. I'd like to change the property of the
class in the collecion.

I'll change the code replacing the "new istance " whith de "old
istance" like method 2....

Thanks again
Sapo
 
L

Lasse Vågsæther Karlsen

Sapo19875 said:
How are you checking those results? The variable CT should contain a
new reference to that (new) "class3" instance. But the value of
myCollectionOfMyClass[0] did not change, that still points to the "old"
CT.

Hans Kesting

thanks Hans for your quickly reply.
the problem is right there. I'd like to change the property of the
class in the collecion.

I'll change the code replacing the "new istance " whith de "old
istance" like method 2....

Thanks again
Sapo

You had the right implementation to begin with:

public override operator +(MyClass class1, MyClass class2)
{
MyClass class3 = new MyClass();
class3.property1 = class1.property1 + class2.property2;
.......
return class3;
}

This will produce a new MyClass instance that contains the result of
adding class1 and class2.
 
S

Sapo19875

You had the right implementation to begin with:

public override operator +(MyClass class1, MyClass class2)
{
     MyClass class3 = new MyClass();
     class3.property1 = class1.property1 + class2.property2;
     .......
     return class3;

}

This will produce a new MyClass instance that contains the result of
adding class1 and class2.

--
Lasse Vågsæther Karlsen
mailto:[email protected]://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Ok Lasse, my goal was to change the vaule of the property stored in
the collection.
I know that the implementation of my method was good (it's the same of
the c# guide....eheh) but,
as i've wrote, when my code return to te caller, the property in the
collection doesn't change...

Bye
 
R

Rene

Well, I realize that I am not answering your question but I could not help
noticing the:

public override operator +(MyClass class1, MyClass class2)

as far as I understand this should be something like this:

public static MyClass operator +( MyClass class1, MyClass class2)

Chances are you where typing your post on the fly and you missed the correct
syntax but in case you didn't and that is how your code really looks like I
would like to know because I am kind of curious.

The other thing to mention is that in your sample code your are adding
proerty1 + proerty2, shouldn't that be property1 + property2?
 
J

Jon Skeet [C# MVP]

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
 
L

Lasse Vågsæther Karlsen

Sapo19875 said:
Ok Lasse, my goal was to change the vaule of the property stored in
the collection.
I know that the implementation of my method was good (it's the same of
the c# guide....eheh) but,
as i've wrote, when my code return to te caller, the property in the
collection doesn't change...

Bye

That is because you're not modifying the collection at all.

Your original + operator takes two objects, from the collection or
otherwise, and produce a new, third, object with the results. It does
not, and should not, modify any of the original two objects passed to it.

As such, the way to get your collection to be modified is to modify the
collection:

myCollectionClass[0] = myCollectionClass[0] + myCollectionClass[1];

or just:

myCollectionClass[0] += myCollectionClass[1];

note that this will replace the original object at element 0 with the
new object that was the result of the addition. The original object,
unless referenced somewhere else, will just be left for the garbage
collector to handle sometime later.

If you actually want to update the object at element 0 instead of
replacing it, the + operator is not what you want, instead add methods
on the object to accumulate the results of adding another object to
itself, ie.

public void Accumulate(MyClass other)
{
property1 += other.property1;
....
}

and then call that instead:

myCollectionClass[0].Accumulate(myCollectionClass[1]);
 
J

Jon Skeet [C# MVP]

Ok Lasse, my goal was to change the vaule of the property stored in
the collection.

In that case you need to change the collection, e.g.

collection[0] += collection[1];
which is shorthand for
collection[0] = collection[0] + collection[1];

That will replace the current element 0 in the collection with the sum
of element 0 and element 1.

The + operator should not change its operands, it should return a new
value. i.e.

x = y + z;

shouldn't change the value of y or z.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top