Pierre Greborio.NET

Talking about .NET world

[Money Pattern] Arithmetic operations

The Money class I implemented is really primitive. I can't imagine a Money class without arithmetic operators. So, the first step is to implement all base operators:

public static Money operator +(Money a, Money b)
{
 return new Money(a._amount + b._amount);
}

Even if the implementation is simple it presents at least one issue. I can't add amount of different currency. Then I could check the currencies and eventually raise an exception if the currency aren't equals:

public static Money operator +(Money a, Money b)
{
 CheckCurrency(a, b);
 return new Money(a._amount + b._amount);
}

private static void CheckCurrency(Money a, Money b)
{
 if (a.Currency != b.Currency)
  throw new ArgumentException("The currencies are not equals.");
}

Comments

TrackBack said:

# March 5, 2005 10:15 PM