Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Overload Operators to Operate on Your Objects : Page 3

Operator overloading provides an intuitive way to support mathematical and comparative operations on your objects.


advertisement
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 <(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 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).



Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing, and developing successful .NET applications. Deborah is the author of several books, including "Doing Objects in Visual Basic 6.0" (SAMS) and "Doing Web Development: Client-Side Techniques" (APress). She is on the INETA Speaker's Bureau and is a well-known speaker at technical conferences.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date