Archive of August 2010
Implementing IComparable
In the most recent project I’m working on at work, we’re using a self-made generator for generating the business entities. The generator creates the class, properties, and implements the IComparable interface.
IComparable contains a CompareTo() method that should return 0 when the objects are equal, a negative value if the current object is less than the other, and positive if the current object is greater than the other.
A generated class looks like this:
public class SomeObject
{
... properties ...
public int CompareTo(SomeObject other)
{
if (Id = = other.Id &&
Name = = other.Name &&
SomeProperty = = other.SomeProperty &&
SomeOtherProperty = = other.SomeOtherProperty)
return 0;
else
return 1;
}
}
It’s a good thing the business entities are not actually compared to each other, but only checked for equality. Sorting these objects will be a downright failure…
Posted in:
Work