1

I can't work out how to write my operator overload. Please help!

I have the following class:

public class Nodegrid<N> where N : INode
{

}

Within Nodegrid functions, I want to be able to write things like

N n1;
N n2;
//...
if (n1 == n2)
//...

But I can't work out how to write the == operator overload for N. I tried overloading INode with

public static bool operator ==(INode n1, INode n2)
{
    return (n1.X == n2.X && n1.Y == n2.Y);
}

but this wasn't sufficient.

I also tried overloading N itself, but I got compiler errors because it was expecting me to overload Nodegrid, not N.

If this is possible, please provide code, if not, please suggest workarounds!

Thanks Haighstrom


UPDATE

For now I have implemented the following workaround on the basis this cannot be done using operator overloads:

public static class NodeExts
{
    public static bool Equals(this INode n1, INode n2)
    {
        return (n1.X == n2.X && n1.Y == n2.Y);
    }
}
Haighstrom
  • 577
  • 5
  • 16
  • You can post answers to your own questions, you just need to wait two days to accept them. If you figure out your own issue there is nothing wrong with posting your own answer and accepting it, it may help someone in who comes to the site a few months or years later who had the same problem as you (and it does not clutter up the front page with a question that has "no accepted answer" when it gets auto-promoted every few weeks). – Scott Chamberlain Nov 02 '13 at 16:22
  • Are we sure this can't be done with operator overloads? I was hoping someone might have a way of doing it. If not, I will write and accept my own answer. – Haighstrom Nov 02 '13 at 16:33
  • That is one of the reasons it makes you wait two days before it lets you accept, someone may post a solution that uses overloads (personally if I posted an answer it would be to just do the extension method solution you already posted). – Scott Chamberlain Nov 02 '13 at 16:36

1 Answers1

0

Edit: this does not solve his problem, was reading the question to quick.


This should do the trick:

public static bool operator ==(Nodegrid<N> n1, Nodegrid<N> n2)
{
    return (n1.X == n2.X && n1.Y == n2.Y);
}
darthmaim
  • 4,970
  • 1
  • 27
  • 40
  • This is comparing Nodegrids, not N's? Nodegrid doesn't have properties of X or Y. – Haighstrom Nov 02 '13 at 16:03
  • Ohh, you are right. Was reading your question to quickly. You cant overload operators in Interfaces, so you would need to change INode from an Interface to an abstract class. See here: http://stackoverflow.com/questions/1881459/how-do-i-override-the-equals-operator-for-an-interface-in-c – darthmaim Nov 02 '13 at 16:08
  • My implementation needs INode to be an interface because the Nodegrid I'm choosing uses N which is already a subclass of something else... – Haighstrom Nov 02 '13 at 16:12
  • I can solve this by writing an IsEqual(INode n1, INode n2) function, but I was hoping to be clever and use operator overloads. – Haighstrom Nov 02 '13 at 16:13
  • You could be clever and use extension methods to write `n1.Equals(n2)`. – darthmaim Nov 02 '13 at 16:15
  • OK I've done that for now unless anyone has any better suggetions. Updated my main question with this solution. Thanks. – Haighstrom Nov 02 '13 at 16:19
  • You can delete your own answer if you realize your answer does not solve the problem. – Scott Chamberlain Nov 02 '13 at 16:24
  • 1
    @ScottChamberlain I thought it would be useful to keep the comments, thats why i didnt delete the answer. – darthmaim Nov 02 '13 at 16:26