TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

 by Deborah Kurata
 Aug 11, 2004
 Page 3 of 3

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

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

.qs-listings #asset-middle-b .group {
width: 70% !important;;
}

.qs-listings #bottomline {
width: 70% !important;
}

```   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.