Overloaded operators are a new feature in Visual Basic 2005 (VB 2005), providing the capa-bility long available in C++ and C#. Just as you can overload methods, you can overload operators such as +, -, and *. In addition to overloading arithmetic operators, you can also create custom conversion operators to convert from one type to another and allow objects to be used in Boolean test expressions.
Overloading operators can make certain classes and structures more natural to use. You should carefully consider the semantics of a type’s operators and not introduce something that is difficult to decipher or maintain. Aim for the most readable code, not only for the next fortunate soul who claps eyes on your code, but also for yourself.
Another reason to take care when overloading operators is that not all .NET languages support overloaded operators, because overloading operators is not part of the Common Language Specification (CLS). For example, VB 2005 is the first version of VB that supports operator overloading. It’s important that your overloaded operators be syntactic shortcuts to functionality provided by secondary methods that perform the same operation and can be called by CLS-compliant languages.
Let’s take a quick look at which operators you can overload. Unary operators, binary opera-tors, and conversion operators comprise the three general types of operators that you can overload in VB. It’s impossible to list all of the conversion operators here, since the set is limit-less (more about conversion operators in the “Conversion Operators” section). Unary operators are those that have only one operand, such as the unary minus in a negative num-ber such as -108. Binary operators have two operands, such as the addition operator in 2 + 2. Table 8-1 lists all of the operators except the conversion operators.
|>=||Binary||Greater than or equal to|
|<=||Binary||Less than or equal to|
|And||Binary||Logical and bitwise And|
|Or||Binary||Logical and bitwise inclusive Or|
|XOr||Binary||Bitwise exclusive Or|
|Not||Unary||Logical Notand one’s complement|