February 21, 2019
Hot Topics:

### Defining an Operator Method

At this point, you have your new class: Distance. You have defined fields, properties, and named methods for operations you'd like to perform on your class. Finally, you can create the operator methods by copying and pasting the named methods and changing the keyword Function to Operator and the method name to operator(symbol), where operator is a literal and symbol represents the actual operator (in this example, "+" and "-"). To complete the implementation, simply call the named methods in the operator methods. Listing 3 shows the additional operator methods.

Listing 3: The Operator+ and Operator: Methods for Distance

```  Public Shared Operator +(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
End Operator

Public Shared Operator -(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
Return Subtract(lhs, rhs)
End Operator

Tip: If you are very comfortable with operator overloading, skip the create named methods step. As for me, after 16 years of OOP programming this is still a useful technique.
```

Listing 4 shows the entire listing of the Distance class and a Main method in a Console application to show you how to invoke the overloaded operators. In this example, "+" and "-" work as you would expect.

Listing 4: The Distance Class with Overloaded Operators and a Sample Main Subroutine

```Module Module1

Sub Main()

Dim i, j, k As Integer
i = 5
j = 7
k = i And j

Console.WriteLine(k)
Dim a As Distance = New Distance(5, 3)
Dim b As Distance = New Distance(6, 13)

Console.WriteLine(a.ToString())
Console.WriteLine(b.ToString())
Dim c As Distance = a + b
Console.WriteLine(c.ToString())

Dim d As Distance = c - b
Console.WriteLine(d.ToString())

Dim e As Distance = d
Console.WriteLine(e.ToString())
End Sub

End Module

Public Class Distance
Private FFeet As Integer
Private FInches As Integer

Public Sub New(ByVal Feet As Integer, ByVal Inches As Integer)
FFeet = Feet
If (Inches > 12) Then
FFeet += (Inches / 12)
FInches = (Inches Mod 12)
Else
FInches = Inches
End If
End Sub

Public Sub New(ByVal Inches As Integer)
FFeet = (Inches / 12)
FInches = (Inches Mod 12)
End Sub

Public Property Feet() As Integer
Get
Return FFeet
End Get
Set(ByVal value As Integer)
FFeet = value
End Set
End Property

Public Property Inches() As Integer
Get
Return FInches
End Get
Set(ByVal value As Integer)
If (value > 12) Then
FFeet += (value / 12)
FInches = (value Mod 12)
Else
FInches = value
End If
End Set
End Property

Public Overrides Function ToString() As String
Return FFeet & "ft " & FInches & "in"
End Function

Public Shared Function Add(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
Return New Distance(lhs.Feet + rhs.Feet, lhs.Inches + rhs.Inches)
End Function

Public Shared Operator +(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
End Operator

Public Shared Function Subtract(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
Return New Distance(lhs.Feet - rhs.Feet, lhs.Inches - rhs.Inches)
End Function

Public Shared Operator -(ByVal lhs As Distance, ByVal rhs As Distance) As Distance
Return Subtract(lhs, rhs)
End Operator
End Class
```

### Intuitive Operators for Your Classes

Operator overloading is harder in C++ because C++ supports overloading tricky member-of operators like member-of (.) and function call (). In addition, C++ supports static and instance operators, but using name methods makes overloading even esoteric operators like () easier.

In VB.NET (and C#) overloaded operators must be shared (static), and operator methods must have the same count as the operator you are overloading. Finally, in VB.NET the special word Operator is used in place of Sub or Function, which makes it very clear that the method you are looking at is an operator statement.

Overloading operators can provide your classes with intuitive operators, and they are easy to implement and use, as long as you don't change the underlying behavior of the operator and use named methods to separate syntax from implementation.

Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book UML DeMystified from McGraw-Hill/Osborne (Spring 2005) and Expert One-on-One Visual Studio 2005 from Wrox (Fall 2005). Paul is also the founder and chief architect for Software Conceptions, Inc, founded 1990. He is available to help design and build software worldwide. You may contact him for consulting opportunities or technology questions at pkimmel@softconcepts.com.

If you are interested in joining, sponsoring a meeting, or posting a job, check out www.glugnet.org, the Web page of the Greater Lansing area Users Group for .NET.

Page 2 of 2

Comment and Contribute

(Maximum characters: 1200). You have characters left.

## Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

## Most Popular Developer Stories

Thanks for your registration, follow us on our social networks to keep up-to-date