perator overloading allows you to define mathematical and comparative operators for your data types. Objects created with your data type can then be manipulated mathematically or compared using standard operators.
Operator overloading is one of those features that you don't need very often, but when you need it, operator overloading is very nice to have. You will find operator overloading in C# now, but you won't find it in Visual Basic until the upcoming Visual Studio 2005 release.
Foreign Currency Price Example
As an example, a Price data type, which supports amounts in any currency, overloads the mathematical operators to allow mathematically manipulating prices in different currencies. In today's global economy, this is a useful feature.
The Price data type in this example is a structure comprised of several fields. The Amount field defines the price amount in a defined currency. The CurrencyCode
field is the three-character code for the currency, such as USD (U.S. dollars), AUD (Australian dollars), or EUR (European Euro).
In a production-level application, the Amount
fields would be enough. The application would use the CurrencyCode
to look up the appropriate currency exchange rates in a table (if current currency rates aren't a requirement) or from a service (if current exchange rates are a requirement).
|By overloading mathematical operators, such as +, -, and * for your data types, developers can add, subtract, multiply, or divide objects created with your data type.|
But writing lookup tables or accessing a service takes away from the focus of this column: operator overloading. So instead, the Price data type in this example has a third field for the exchange rate.
As you know, every currency has an exchange rate with respect to every other currency. The standard way to operate on two prices defined with two different currencies is to find the exchange rate between the two currencies, convert one currency to the other, and then perform the operation. This allows you to add "apples and apples" as the saying goes.
However, any one particular Price object in this example would not know what other currency it may need to convert to. So it could not store just one exchange rate, it would need to store an exchange rate for every possible other currency.
A better approach is to define a base currency. Every Price object stores the exchange rate with respect to the base currency. The application then converts each Price amount to the base currency, performs the operation, and converts back to the desired currency. This example selected USD as the base currency, but you can use any currency for this purpose.
The structure below defines the three fields of the Price data type.
public struct Price
public float Amount;
public string CurrencyCode;
public float ExchangeRateWRTUSD;
The constructor for this structure sets the values of the fields:
public Price(float fAmount, string sCurrencyCode,
this.Amount = fAmount;
this.ExchangeRateWRTUSD = fExchangeRateWRTUSD;