I’m pretty sure this is a basic question but for some reason i can’t find it on Google as quick as I’d like so I’ll ask it here.
I’ve got a MoneyType object, which holds an int (value in cents) and a decimal (just the normal value)
This type inherits the IComperable interface and can be compared, now i’m comparing two MoneyTypes in an If statement:
if (invoice.GrossAmount == invoice.NetAmount)
{
//Something
}
Now for what method should i override or what interface should i inherit to get this to work? Since doing this does not enter the CompareTo() method, neither does it enter the .Equals method so I’m at a loss atm.
If you implement
IComparable, than you implement theCompareTomethod. Based on this implementation, testing two amounts for equality is performed as:Comparing directly with “==” means overloading the “==” operator as described here. That is add the following method inside the
MoneyTypeclass:You must implement “!=”, too. You should also implement the Equals method (simply call the == operator) and the
GetHashCode.HTH,
Lucian