5.7. Operator OverloadingNew 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.
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. |