S
Steve Richter
Can I get the C# compiler to implicity implement the relational
operators when my class overrides the Equals method and implements the
IComparer interface?
coding operator== is a bit of work since I have to first cast to
object and compare for null. Then compare for equality. ( is that
true? )
thanks,
( here is the sample class I a working with. No methods are called in
it when operator== is used. )
using System;
using System.Collections.Generic;
using System.Text;
namespace demo_Notepad
{
public class DemoClass1 : IComparer<DemoClass1>
{
public int Row = 0;
public int Column = 0;
public DemoClass1()
{
}
public DemoClass1(int InRow, int InColumn)
{
Row = InRow;
Column = InColumn;
}
public override string ToString()
{
return Row.ToString() + ", " + Column.ToString();
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
else
{
int rv = Compare(this, (DemoClass1)obj);
return (rv == 0);
}
}
#region IComparer<DemoClass1> Members
public int Compare(DemoClass1 InFac1, DemoClass1 InFac2)
{
if (InFac1.Row < InFac2.Row)
return -1;
else if (InFac1.Row > InFac2.Row)
return 1;
else if (InFac1.Column < InFac2.Column)
return -1;
else if (InFac1.Column > InFac2.Column)
return 1;
else
return 0;
}
#endregion
}
}
operators when my class overrides the Equals method and implements the
IComparer interface?
coding operator== is a bit of work since I have to first cast to
object and compare for null. Then compare for equality. ( is that
true? )
thanks,
( here is the sample class I a working with. No methods are called in
it when operator== is used. )
using System;
using System.Collections.Generic;
using System.Text;
namespace demo_Notepad
{
public class DemoClass1 : IComparer<DemoClass1>
{
public int Row = 0;
public int Column = 0;
public DemoClass1()
{
}
public DemoClass1(int InRow, int InColumn)
{
Row = InRow;
Column = InColumn;
}
public override string ToString()
{
return Row.ToString() + ", " + Column.ToString();
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
else
{
int rv = Compare(this, (DemoClass1)obj);
return (rv == 0);
}
}
#region IComparer<DemoClass1> Members
public int Compare(DemoClass1 InFac1, DemoClass1 InFac2)
{
if (InFac1.Row < InFac2.Row)
return -1;
else if (InFac1.Row > InFac2.Row)
return 1;
else if (InFac1.Column < InFac2.Column)
return -1;
else if (InFac1.Column > InFac2.Column)
return 1;
else
return 0;
}
#endregion
}
}