Re: Overloading binary + operator

Tech-Archive recommends: Fix windows errors by optimizing your registry



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
.



Relevant Pages

  • Re: [incr Tcl] state of this project?
    ... yes; look at the following snipplet; it creates ... a class named "MyClass" and adds an instance method named ... "foo" to it. ... For real code, one should ...
    (comp.lang.tcl)
  • Re: Why where new() doesnt allow parameters?
    ... signatures this way in real code, but I've used it on several occations to ensure that a generic parameter is serializable. ... static MyClass() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to terminate an object thats in a collection
    ... Another way to refer to multiple instances of the same class would be with ... Dim arrClassas Class1 ... Coll.Remove 1 'removes item from collection, but MyClass still ... Removing an object from a collection won't remove it from memory, ...
    (microsoft.public.excel.programming)
  • Re: Jscript executing
    ... Hard to tell without seeing the content of Class1 ... Private Sub Form_Load ... Dim MyClass As Class1 ... Set MyClass = New Class1 ...
    (comp.lang.basic.visual.misc)
  • Re: How to terminate an object thats in a collection
    ... Dim col As Collection ... Dim MyClass As Class1 ... Set MyClass = Nothing ' or goes out of scope ...
    (microsoft.public.excel.programming)