Reply to thread

If you use the built in type Point (part of System.Drawing) you wont have to overload - they work as expected.


If you just want to see how to implement operator overloading, heres a sample:

[code=csharp]

struct MyStruct

{

    public int x;

    public int y;


    public MyStruct(int x, int y)

    {

        this.x = x;

        this.y = y;

    }


    public static bool operator ==(MyStruct v1, MyStruct v2)

    {

        return (v1.x==v2.x && v1.y==v2.y);

    }

    public static bool operator !=(MyStruct v1, MyStruct v2)

    {

        return (v1.x!=v2.x || v1.y!=v2.y);

    }

}

[/code]


And heres the sample code to test:

[code=csharp]

MyStruct m1 = new MyStruct(1, 2);

MyStruct m2 = new MyStruct(3, 4);

MyStruct m3 = new MyStruct(1, 2);


// The following is true;

if(m1 == m3) Debug.WriteLine("=");

// The following is not true

if(m1 == m2) Debug.WriteLine("=");

[/code]


-nerseus


Back
Top