Operator Overloading


Visual Basic defines operators for expressions that use standard data types such as integers and Boolean values. It defines a few operators such as Is and IsNot for objects, but operators such as * and Mod don’t make sense for objects in general.

However, you can also define those operators for your structures and classes by using the Operator statement. This is a more advanced topic, so if you’re new to Visual Basic, you may want to skip this section and come back to it later, perhaps after you have read Chapter 16.

The general syntax for operator overloading is:

  [ <attributes> ] Public [ Overloads ] Shared [ Shadows ] _  [ Widening | Narrowing ]  Operator symbol ( operands ) As type     ... End Operator 

The parts of this declaration are:

  • attributes - Attributes for the operator.

  • Public - All operators must be Public Shared.

  • Overloads - You can only use this if the operator takes two parameters that are from a base class and a derived class as its two operators. In that case, it means the operator overrides the operator defined in the base class.

  • Shared - All operators must be Public Shared.

  • Shadows - The operator replaces a similar operator defined in the base class.

  • Widening - Indicates that the operator defines a widening conversion that always succeeds at runtime. This operator must catch and handle all errors. The CType operator must include either the Widening or Narrowing keyword.

  • Narrowing - Indicates that the operator defines a narrowing conversion that may fail at runtime. The CType operator must include either the Widening or Narrowing keyword.

  • symbol - The operator’s symbol. This can be +, -, *, /, \, ^, &, <<, >>, =, <>, <, >, <=, >=, Mod, Not, And, Or, Xor, Like, IsTrue, IsFalse, or CType.

  • operands - Declarations of the objects to be manipulated by the operator. The unary operators +, -, Not, IsTrue, and IsFalse take a single operand. The binary operators +, -, *, /, \, ^, &, <<, >>, =, <>, <, >, <=, >=, Mod, And, Or, Xor, Like, and CType take two operands.

  • type - All operators must have a return type and must return a value by using a Return statement.

Operator overloading is subject to several constraints:

  • Some operands come in pairs, and if you define one you must define the other. The pairs are = and <>, < and >, <= and >=, and IsTrue and IsFalse.

  • For the standard unary or binary operators, the class or structure that defines the operator must appear in an operand. For the CType conversion operator, the class or structure must appear in the operand or return type.

  • The IsTrue and IsFalse operators must return Boolean values.

  • The second operands for the << and >> operators must be Integers.

If you define an operator, Visual Basic can automatically handle the same operator followed by the = sign. For example, if you define the + operator, Visual Basic can understand the += assignment operator.

While you cannot use the IsTrue and IsFalse operators directly, you can use them indirectly. If you define IsTrue for a class, Visual Basic uses it to determine whether an object should be treated as True in a Boolean expression. For example, the following statement uses the IsTrue operator to decide whether the object c1 should be considered True:

  If c1 Then ... 

If you define the And and IsFalse operators, Visual Basic uses them to handle the AndAlso operator as well. For this to work, the And operator must return the same type of class or structure where you define it. For example, suppose you have defined And and IsFalse for the Composite class and suppose variables c1, c2, and c3 are all instances of this class. Then consider the following statement:

  c3 = c1 AndAlso c2 

Visual Basic uses IsFalse to evaluate c1. If IsFalse returns True, the program doesn’t bother to evaluate c2. Instead it assumes the whole statement is false and returns a False value. Because IsFalse returned True for c1, Visual Basic knows that c1 is a false value so it sets c3 equal to c1.

This is pretty confusing. It may make more sense if you think about how Visual Basic evaluates Boolean expressions that use the normal AndAlso operator.

Similarly, if you define the Or and IsTrue operators, Visual Basic automatically provides the OrElse operator.

Although you generally cannot make two versions of a function in Visual Basic that differ only in their return types, you can do that for CType conversion operators. When the program tries to make a conversion, Visual Basic can tell by the type of the result which conversion operator to use.

The following code shows a Complex class that represents a complex number. It defines +, -, and * operators to implement normal addition, subtraction, and multiplication on complex numbers. It also defines =, <>, and unary negation operators, and a conversion operator that converts a Complex object into a Double by returning its magnitude.

  Public Class Complex     Public Re As Double     Public Im As Double     ' Constructors.     Public Sub New()     End Sub     Public Sub New(ByVal real_part As Double, ByVal imaginary_part As Double)         Re = real_part         Im = imaginary_part     End Sub     ' ToString.     Public Overrides Function ToString() As String         Return Re.ToString & " + " & Im.ToString & "i"     End Function     ' Operators.     Public Shared Operator *(ByVal c1 As Complex, ByVal c2 As Complex) As Complex     Return New Complex( _         c1.Re * c2.Re - c1.Im * c2.Im, _         c1.Re * c2.Im + c1.Im * c2.Re)     End Operator     Public Shared Operator +(ByVal c1 As Complex, ByVal c2 As Complex) As Complex         Return New Complex( _             c1.Re + c2.Re, _             c1.Im + c2.Im)     End Operator     Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As Complex) As Complex         Return New Complex( _             c1.Re - c2.Re, _             c1.Im - c2.Im)     End Operator     Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean         Return (c1.Re = c2.Re) AndAlso (c1.Im = c2.Im)     End Operator     Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean         Return (c1.Re <> c2.Re) OrElse (c1.Im <> c2.Im)     End Operator     Public Shared Operator -(ByVal c1 As Complex) As Complex         Return New Complex(-c1.Re, -c1.Im)     End Operator     Public Shared Narrowing Operator CType(ByVal c1 As Complex) As Double         Return System.Math.Sqrt(c1.Re * c1.Re + c1.Im * c1.Im)     End Operator End Class 

It is easy to get carried away with operator overloading. Just because you can define an operator for a class doesn’t mean you should. For example, you might be able to concoct some meaning for addition with the Employee class, but it would probably be a counterintuitive operation. You would probably be better off writing a subroutine or function with a meaningful name.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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