Section 5.7. Operator Overloading


5.7. Operator Overloading

New in 2005. Although Visual Basic is as powerful as any other .NET language, early versions lacked specific features found in some other .NET languages (just as VB had features absent from those languages). One feature present in C#, but absent in VB, was operator overloading , the ability to redefine unary and binary operators and give them special uses when working with specific classes or structures. As of the 2005 release of Visual Basic, operator overloading is now part of the Visual Basic experience.

To perform operator overloading, you simply create a special procedure in your class with the name of the operator, indicate the data type(s) of the operand(s) and the data type of the return value, make it Public and Shared, and it's ready to use. The class (or structure) you put the procedure in is significant. At least one operand for the operator must be of the class data type in which the procedure appears. For the special CType unary operator, either the operand or its return value must use the data type of the class that includes the procedure.

All overloaded operator procedures share a common syntax.

     Public Shared [otherModifiers] Operator operatorSymbol _           (ByVal operand1 As dataType[, ByVal operand2 As dataType]) _           As returnDataType        ' ----- Statements of the operator procedure.     End Operator 

As an example, consider a class named LandRegion that defines the boundaries of a piece of land. Since you would like to merge two records together into a larger tract of land using the + addition operator, you define the following procedure in the LandRegion class.

     Public Shared Operator +(ByVal firstArea As LandRegion, _           ByVal secondArea As LandRegion) As LandRegion        ' ----- Merge two land regions together.        Dim combinedRegion As New LandRegion        ' ...more code here...        Return combinedRegion     End Operator 

Since the routine is Public, it is available to your entire program. Since it is Shared, the routine exists even without the presence of any specific instance of the class (although at least one operand must be of that class). The defined operands must always be passed ByVal. Using this operator is simple.

     Dim mainCity As New LandRegion     Dim unincorporatedArea As New LandRegion     Dim annexation As LandRegion     ' ...fill in mainCity and unincorporatedArea members, then...     annexation = mainCity + unincorporatedArea 

For binary operators, only one of the operands has to match the enclosing class or structure type.

     Public Shared Operator +(ByVal wholeOrder As OrderRecord, _           ByVal orderDetailItem As DetailRecord) As OrderRecord        ' ----- Append a new product item onto the order.        ' ...more code here...     End Operator 

Table 5-1 describes the operators that can be overloaded.

Table 5-1. Operators that can be overloaded

Operator

Description

+

Unary Plus operator. It differs from the binary addition operator in that you supply only one operand in the procedure signature.

-

Unary Negation operator. It differs from the binary subtraction operator in that you supply only one operand in the procedure signature.

Not

Bitwise Negation operator. For overloading, this is a bitwise operation only, not logical.

IsTrue

If you overload the Or operator in a class, overloading the IsTrue operator in the same class opens up the use of the OrElse operator with the class. You must also overload the IsFalse operator. The overload procedure's return type must be Boolean.

IsFalse

If you overload the And operator in a class, overloading the IsFalse operator in the same class opens up the use of the AndAlso operator with the class. You must also overload the IsTrue operator. The overload procedure's return type must be Boolean.

+

Binary Addition operator. It differs from the unary plus operator in that you supply two operands in the procedure signature.

-

Binary Subtraction operator. It differs from the unary negation operator in that you supply two operands in the procedure signature.

*

Multiplication operator.

/

Division operator.

\

Integer Division operator.

Mod

Modulo operator.

&

Concatenation operator.

^

Exponentiation operator.

<<

Shift Left operator. The second operator must use the Integer data type.

>>

Shift Right operator. The second operator must use the Integer data type.

=

Equal To comparison operator. You must also overload the <> Not Equal To operator.

<

Less Than comparison operator. You must also overload the > Greater Than operator.

>

Greater Than comparison operator. You must also overload the < Less Than operator.

<=

Less Than or Equal To comparison operator. You must also overload the >= Greater Than or Equal To operator.

>=

Greater Than or Equal To comparison operator. You must also overload the <= Less Than or Equal To operator.

<>

Not Equal To comparison operator. You must also overload the = Equal To operator.

And

Bitwise Conjunction operator. For overloading, this is a bitwise operation only, not logical.

Or

Bitwise Disjunction operator. For overloading, this is a bitwise operation only, not logical.

Xor

Bitwise Exclusion operator. For overloading, this is a bitwise operation only, not logical.

Like

Pattern Comparison operator.

CType

Unary Conversion operator. Used to convert data from one data type (or class or structure) to another. You must include either the Narrowing or Widening keyword in the definition of the overload, somewhere between the Shared and Operator keywords. These modifiers tell the compiler what type of conversion is allowed. Narrowing conversions may fail if the destination data type cannot support the value of the source data type. Either the operand or the return type of the overload procedure must be of the class or structure that contains the procedure.


When you overload operators, you can define them to do whatever you want with the source classes in question. In fact, you could create a class where the normal understandings of addition and subtraction were reversed. However, such practices will make the code more difficult to understand and debug.

For further details about operator overloading, see the "Operator Statement" entry in Chapter 12.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net