What Is Operator Overloading?


Operator overloading allows your code to enhance the basic Visual Basic operators, and endow them with abilities not previously granted to them by the compiler. Overloading doesn't change the syntax used when employing operators, but it does change the types of objects that each operator can manage. For instance, the multiplication operator (*) normally only interacts with numbers, but you can augment it to work with your own custom Bumblebee class.

Dim swarm As Bumblebee Dim oneBumblebee As New Bumblebee Dim twoBumblebee As New Bumblebee swarm = oneBumblebee * twoBumblebee 


The meaning you apply to the overloaded operator is up to you. Although you would normally want to retain the additive nature of the addition operator when overloading it, you don't have to. In fact, you could overload the addition operator so that it subtracts one value from another. But I'd fire you if you did that working for me. Just so you know.

All operator overloading features tie directly to one or more of your classes. Overloaded features look curiously like standard function members, and appear as members of your classes.

Visual Basic includes two types of operators: unary and binary, defined based on the number of operands recognized by the operator. Unary operators accept a single operand, which always appears to the right of the operator name or symbol. The logical Not operator is a unary operator.

oppositeValue = Not originalValue 


Binary operators accept two operands, one on each side of the operator. The multiplication operator is a binary operator.

ten = two * five 


The nature of an operator is that once it has done its work, the operator and its input operand(s) are, in effect, fully replaced by the calculated result. The expression "10 / 5" is replaced by the calculated "2" result, and this result is used to complete whatever statement or expression the original operation appeared in. It works just like functions.

' ----- These two lines (probably) place the same '       calculated result in theAnswer. theAnswer = 2 * 5 theAnswer = DoubleIt(5) 


To get ready for operator overloading, begin to alter your mind to see operators as functions. Look past the confines of your operator universe, and open your thoughts to the truth that the operators and the functions are one. If you've ever programmed in LISP, then I truly feel sorry for you. But you also already understand operators as functions. In LISP, everything is a function. To multiply two numbers together in LISP, you use "prefix" syntax, where the operator name comes first. The expression "seven times three" uses this syntax.

(* 7 3) 


Once complete, the entire parenthesized expression is replaced, function-like, by its answer. The LISP expression:

(+ 2 (* 7 3)) 


becomes:

(+ 2 21) 


becomes:

23 


Defining overloaded operators in Visual Basic 2005 is somewhat similar. If you were to translate the definition of multiplication into Visual Basic function-ese, it might look like this:

Public Shared Function *( _    ByVal firstOperand As Integer, _    ByVal secondOperand As Integer) _    As Integer 


The operator (*) becomes a function name, with operands playing the role of function arguments, ultimately generating a value exposed through the function's return value. Although operators aren't defined as functions in this way in Visual Basic, overloads of those operators are.

To overload the multiplication operator in our imaginary Bumblebee class, we use the new Operator keyword to define a "multiplication function" for operands of the Bumblebee class.

Partial Class Bumblebee    Public Shared Operator *(ByVal operand1 As Bumblebee, _          ByVal operand2 As Bumblebee) As Bumblebee       ' ----- Multiply two bumblebees together.       Dim finalResult As New Bumblebee       ' ----- Add special "multiplication" code here, then...       Return finalResult    End Operator End Class 


Now, when you multiply two Bumblebee instances together with the multiplication operator, Visual Basic recognizes the "operand1 * operand2" pattern as matching a multiplication operator overload with two Bumblebee arguments, and calls this class-based Operator function to get the result.

All Operator declarations must include the Public and Shared keywords. If they weren't shared, Visual Basic would be required to create an extra instance of the class just to access the operator overload code, and that wouldn't be very efficient.




Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

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