Other Operator Overloading Issues


There are a few other rules you must follow when overloading operators, but first let's look at a semi-useful Bumblebee class.

Class Bumblebee    Public Bees As Integer    Public Sub New()       ' ----- Default constructor.       Bees = 0    End Sub    Public Sub New(ByVal startingBees As Integer)       ' ----- Assign an initial number of bees.       Bees = startingBees    End Sub    Public Shared Operator +(ByVal operand1 As Bumblebee, _          ByVal operand2 As Bumblebee) As Bumblebee       ' ----- Join bumblebee groups.       Dim newGroup As New Bumblebee       newGroup.Bees = operand1.Bees + operand2.Bees       Return newGroup    End Operator    Public Shared Operator -(ByVal operand1 As Bumblebee, _          ByVal operand2 As Bumblebee) As Bumblebee       ' ----- Separate bumblebee groups.       Dim newGroup As New Bumblebee       newGroup.Bees = operand1.Bees - operand2.Bees       If (newGroup.Bees < 0) Then newGroup.Bees = 0       Return newGroup    End Operator    Public Shared Operator *(ByVal operand1 As Bumblebee, _          ByVal operand2 As Bumblebee) As Bumblebee       ' ----- Create a swarm.       Dim newGroup As New Bumblebee       newGroup.Bees = operand1.Bees * operand2.Bees       Return newGroup    End Operator    Public Shared Widening Operator CType( _          ByVal operand1 As Bumblebee) As Integer       ' ----- Perform conversion here.       Return operand1.Bees    End Operator End Class 


The class is pretty simple; it exists to maintain a simple count of bees. But by overloading the addition, subtraction, multiplication, and CType operators, we can use instances of bees with a more natural syntax.

Dim studyGroup1 As New Bumblebee(20) Dim studyGroup2 As New Bumblebee(15) Dim swarmGroup As Bumblebee = studyGroup1 * studyGroup2 MsgBox("The swarm contains " & CInt(swarmGroup) & " bees.") 


Running this code correctly generates a 300-bee swarm and the message in Figure 12-2.

Figure 12-2. Bees sure know how to multiply


Including a CType overload that generated an Integer allowed me to convert a Bumblebee using the CInt operator. I could also have changed the last line to use the true CType operator.

MsgBox("The swarm contains " & CType(swarmGroup, _    Integer) & " bees.") 


Declaration Requirements

As mentioned earlier, you must always make Operator methods Public Shared. And because the overloaded operators need some sort of intimate connection to their containing class, at least one of the operands or the return value must match the type of the containing class. (In some overloads, Visual Basic requires that it be one of the operands that match.) Either of the two following overloads will work just fine, because Bumblebee is used for one of the operands.

Public Shared Operator <=(ByVal operand1 As Bumblebee, _       ByVal operand2 As Integer) As Boolean    ' ----- Compare a bumblebee to a value. End Operator Public Shared Operator <=(ByVal operand1 As Date, _       ByVal operand2 As Bumblebee) As Boolean    ' ----- Compare a date to a bumblebee. End Operator 


However, you cannot set both operands to a non-Bumblebee type at the same time and still keep the overload in the Bumblebee class.

Class Bumblebee    Public Shared Operator <=(ByVal operand1 As Date, _          ByVal operand2 As Integer) As Boolean       ' ----- This will not compile.    End Operator End Class 


Overloading Overloads

You can overload overloaded operators. No, dear editor, I didn't type the same word twice by mistake. You can add multiple argument-and-return-value signature variations of an overloaded operator to a single class.

Public Shared Widening Operator CType( _       ByVal operand1 As Bumblebee) As Integer    ' ----- Perform conversion to Integer here. End Operator Public Shared Widening Operator CType( _       ByVal operand1 As Bumblebee) As Date    ' ----- Perform conversion to Date here, somehow. End Operator 


As long as the argument signatures or return values differ, you can add as many overloads of an operator as you want. You don't need to use the Overloads keyword either.

Be Nice

That's right. Be nice. Just because you have the power to redefine addition to be division, you don't have to be so shocking. Don't make the maintenance programmers who have to modify your code later work harder because of your mischievous operator overloads. And I and my fellow maintenance programmers will thank you.




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

Similar book on Amazon

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