Types of Operators in Visual Basic - Operator Overloading
(Page 3 of 4 )
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 Oroperator in a class, overloading the IsTrueoperator in the same class opens up the use of the OrElse operator with the class. You must also overload the IsFalse operator. The over-load procedure’s return type must be Boolean. |
IsFalse | If you overload the Andoperator in a class, overloading the IsFalseoperator in the same class opens up the use of the AndAlsooperator with the class. You must also overload the IsTrueoperator. 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 Integerdata type. |
>> | Shift Right operator. The second operator must use the Integerdata 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 Narrowingor Wideningkeyword in the definition of the overload, some-where between the Sharedand Operatorkeywords. 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.
Next: Operator Precedence >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter five of the book Visual Basic 2005 in a Nutshell, Third Edition, written by Tim Patrick, Steven Roman, Ph.D., Ron Petrusha and Paul Lomax (O'Reilly; ISBN: 059610152X). Check it out today at your favorite bookstore. Buy this book now.
|
|