Overload Operators to Operate on Your Objects

Overload Operators to Operate on Your Objects

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 and CurrencyCode 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,      float fExchangeRateWRTUSD)   {      this.Amount = fAmount;      this.CurrencyCode=sCurrencyCode;      this.ExchangeRateWRTUSD = fExchangeRateWRTUSD;   }

Mathematical Operators
By overloading mathematical operators, such as +, -, and * for your data types, developers can add, subtract, multiply, or divide objects created with your data type.

To provide for the addition of two Price objects, you could define an Add method and pass it to two Price objects. But it would be more intuitive if you could simply add the two Price objects with the + operator. That is the purpose of operator overloading.

The code to overload an operator uses a static method with the operator keyword to define the method as an overloaded operator.

   public static Price operator +(Price p1, Price p2)   {      Price p3;      //Convert both currencies to a base currency      float p1InUSD = p1.Amount * p1.ExchangeRateWRTUSD;      float p2InUSD = p2.Amount * p2.ExchangeRateWRTUSD;         // Add the two prices in the base currency      // Convert the result to the first currency type      float p3Converted = (p1InUSD + p2InUSD) /          p1.ExchangeRateWRTUSD;      p3 = new Price(p3Converted,         p1.CurrencyCode,         p1.ExchangeRateWRTUSD);      }      return p3;   }

This method converts each amount to the base currency (USD), adds the amounts, and then converts the result back to the currency of the first Price object.

You can make this method smarter. If the two exchange rates are the same, it could bypass the conversions.

   //If both exchange rates are the same type,    //just add them   if (p1.ExchangeRateWRTUSD ==       p2.ExchangeRateWRTUSD)      {      p3 = new Price(p1.Amount+p2.Amount,         p1.CurrencyCode,         p1.ExchangeRateWRTUSD);      }         else      {         //Convert both currencies to a base currency       }

You use the overloaded operator with your data type just like any other data type. Simply add Price objects together.

   Price keyboardPrice =       new Price(40,"AUS",(float).6);   Price mousePrice =       new Price(20, "EUR", (float).8);   Price Total = keyboardPrice + mousePrice;   MessageBox.Show("Total Order Price is: " +      Total.Amount.ToString());

You would use similar code to overload the , *, and / operators. I’ll leave that code as an exercise for the reader.

Assignment Operators
You cannot directly overload assignment operators such as equal (=). However, you can achieve operator overloading for the addition assignment operator (+=) and the related assignment operators (-=, *=, /=) using the mathematical operators.

   Price speakerPrice = new Price(30, "AUS",      (float).6);   Price surCharge = new Price(5, "AUS",       (float).6);   speakerPrice += surCharge;   MessageBox.Show("Total Cost for Speakers is: " +      speakerPrice.Amount.ToString());

This code defines a surcharge amount and increments the price by the surcharge amount.

You must overload comparison operators in pairs.

If you implemented the other mathematic operators, you can use this technique for their corresponding assignment operators.

Comparison Operators
You can also overload comparison operators, such as equal (=), not equal (!=), less than (), greater than (>), less than or equal to () or greater than or equal to (>=).

The code to overload the greater than (>) operator returns a Boolean value, true or false.

   public static bool operator >(Price p1, Price p2)   {      //If both exchange rates are the same type,      // just compare them      if (p1.ExchangeRateWRTUSD ==          p2.ExchangeRateWRTUSD)      {         return (p1.Amount > p2.Amount);      }      else      {         //Convert both currencies to a base currency         float p1InUSD = p1.Amount *             p1.ExchangeRateWRTUSD;         float p2InUSD = p2.Amount *             p2.ExchangeRateWRTUSD;         return (p1InUSD > p2InUSD);      }   }

This code first compares the exchange rates. If the rates are the same it returns true if the first Price amount is greater than the second Price amount. If the exchange rates are different, it converts both currencies to the base currency and then performs the comparison.

You must overload comparison operators in pairs, that is, if you overload == you must also overload !=. Since the example overloaded the greater than operation (>) it must also overload the less than operation ().

   public static bool operator 

This code is almost identical to the code that overloads the greater than operator.

You cannot overload the conditional logical operators (&& and ||) or the cast operator ().

Operator overloading provides a way to build data types that support mathematical and comparison operators. Developers that define objects using your data type can then use standard operators as an intuitive way to manipulate the objects.

Look for many more operator overloading features in the upcoming version of Visual Studio 2005.

This article is from Deborah Kurata's Best Kept Secrets in .NET book that is in progress (Apress 2004).


Share the Post: