Recipe 3.15. Using Standard Operators for Nonstandard Purposes


Problem

The basic Visual Basic operators, such as the addition operator (+), seem so useful that you would like to use them for your own custom classes.

Solution

Use operator overloading, a new feature in Visual Basic 2005, to allow your own classes to interact with each other through the standard Visual Basic operators.

Discussion

Operator overloading extends method overloading to include the standard Visual Basic operators. In a way, it treats operators such as +, *, and Or as method names. Consider a class that manages scientific specimens:

 Class Specimen 

If your application supports the idea of combining two specimens, resulting in a merged yet single larger specimen, it would be great to be able to use the addition operator to merge two distinct specimens into a single combined specimen:

 Dim part1 As New Specimen Dim part2 As New Specimen Dim combinedParts As Specimen '…later… combinedParts = part1 + part2 

To add support for addition to this class, overload the + operator by adding an Operator definition to the class:

 Public Shared Operator +(ByVal firstPart As Specimen, _       ByVal secondPart As Specimen) As Specimen    Dim mergedSpecimen As New Specimen    ' ----- Add logic to merge the two parts, then…    Return mergedSpecimen End Operator 

You can include different input or output types in the overloaded function, as long as at least one input or output matches the class in which the overload appears:

 Public Shared Operator +(ByVal singlePage As Page, _       ByVal sourceBook As Book) As Book    ' ----- Adds a page to a book. End Operator 

All overloaded operators must include the Shared keyword in the definition.

For unary operators, such as the Not operator, only a single argument is sent to the overloaded function. Table 3-1 lists the overloadable operators.

Table 3-1. Overloadable operators

Operator

Description

+(Unary)

Unary plus operator, as in the expression "+5." Unary plus is seldom used in standard Visual Basic programming, but you can use it for your own classes.

 Shared Operator +(ByVal arg1 As Type) As Type 

-(Unary)

Unary negation operator, as in "-5."

 Shared Operator -(ByVal arg1 As Type) As Type 

+

Addition operator, used to "add" items together.

 Shared Operator +(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

-

Subtraction operator, used to "subtract" one item from another.

 Shared Operator -(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

*

Multiplication operator.

 Shared Operator *(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

/

Division operator.

 Shared Operator /(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

\

Integer division operator.

 Shared Operator \(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

Mod

Modulo operator.

 Shared Operator Mod(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

&

Concatenation operator.

 Shared Operator &(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

^

Exponentiation operator.

 Shared Operator ^(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

<<

Shift left operator. Since the operand to the right of the standard operator is always an Integer, the second argument passed to the overload is also an Integer.

 Shared Operator <<(ByVal arg1 As Type, _    ByVal arg2 As Integer) As Type 

>>

Shift right operator. Since the operand to the right of the standard operator is always an Integer, the second argument passed to the overload is also an Integer.

 Shared Operator >>(ByVal arg1 As Type, _    ByVal arg2 As Integer) As Type 

=

Equal to comparison operator, for use in If and similar statements. You must also overload the related <> (not equal to) operator.

 Shared Operator =(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

<

Less than comparison operator, for use in If and similar statements. You must also overload the related > (greater than) operator.

 Shared Operator <(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

>

Greater than comparison operator, for use in If and similar statements. You must also overload the related < (less than) operator.

 Shared Operator >(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

<=

Less than or equal to comparison operator, for use in If and similar statements. You must also overload the related >= (greater than or equal to) operator.

 Shared Operator <=(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

>=

Greater than or equal to comparison operator, for use in If and similar statements. You must also overload the related <= (less than or equal to) operator.

 Shared Operator >=(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

<>

Not equal to comparison operator, for use in If and similar statements. You must also overload the related = (equal to) operator.

 Shared Operator <>(ByVal arg1 As Type, _    ByVal arg2 As Type) As Boolean 

Not

Bitwise negation operator.

 Shared Operator Not(ByVal arg1 As Type) As Type 

IsTrue

Used to support overloading of the OrElse operator. You must also overload the related IsFalse operator, and you will probably want to overload Or as well.

 Shared Operator IsTrue(ByVal arg1 As Type) _    As Boolean 

IsFalse

Used to support overloading of the AndAlso operator. You must also overload the related IsTrue operator, and you will probably want to overload And as well.

 Shared Operator IsFalse(ByVal arg1 As Type) _    As Boolean 

And

Bitwise conjunction operator.

 Shared Operator And(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

Or

Bitwise disjunction operator.

 Shared Operator Or(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

Xor

Bitwise exclusion operator.

 Shared Operator Xor(ByVal arg1 As Type, _    ByVal arg2 As Type) As Type 

Like

Pattern comparison operator. The second operator is always a pattern string.

 Shared Operator Like(ByVal arg1 As Type, _    ByVal arg2 As String) As Boolean 

CType

Type conversion operator, for converting between different core and custom data types. Visual Basic supports two types of conversions: narrowing and widening. In narrowing conversions there is a chance that the source data will not fit in the target data type, as when converting a Long to an Integer. Conversions in the other direction are widening, and these never result in data loss. You must specify the type of conversion using the Narrowing or Widening keyword.

 Shared [Narrowing | Widening] Operator _    CType(ByVal sourceData As Type) As Type 


You can overload overloaded operators. That is, you can include multiple overloads for, say, the addition (+) operator in a single class, as long as the argument signatures differ.

While operator overloading can make your code more straightforward, it can also add a level of confusion, since you will be using operators in a way that is not part of the standard language usage. Where there is the possibility of confusion, add meaningful comments to the code to guide the reader through the rough spots.

See Also

Recipe 3.14 discusses standard method overloading.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

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