How to: Define an Operator (Visual Basic)
If you have defined a class or structure, you can define the behavior of a standard operator (such as *, <>, or And) when one or both of the operands is of the type of your class or structure.
Define the standard operator as an operator procedure within the class or structure. All operator procedures must be Public Shared.
Defining an operator on a class or structure is also called overloading the operator.
The following example defines the + operator for a structure called height. The structure uses heights measured in feet and inches. One inch is 2.54 centimeters, and one foot is 12 inches. To ensure normalized values (inches < 12.0), the constructor performs modulo 12 arithmetic. The + operator uses the constructor to generate normalized values.
Public Shadows Structure height ' Need Shadows because System.Windows.Forms.Form also defines property Height. Private feet As Integer Private inches As Double Public Sub New(ByVal f As Integer, ByVal i As Double) Me.feet = f + (CInt(i) \ 12) Me.inches = i Mod 12.0 End Sub Public Overloads Function ToString() As String Return Me.feet & "' " & Me.inches & """" End Function Public Shared Operator +(ByVal h1 As height, ByVal h2 As height) As height Return New height(h1.feet + h2.feet, h1.inches + h2.inches) End Operator End Structure