An operator procedure is a series of Visual Basic statements that define the behavior of a standard operator (such as *, <>, or And) on a class or structure you have defined. This is also called operator overloading.
When to Define Operator Procedures
When you have defined a class or structure, you can declare variables to be of the type of that class or structure. Sometimes such a variable needs to participate in an operation as part of an expression. To do this, it must be an operand of an operator.
Visual Basic defines operators only on its fundamental data types. You can define the behavior of an operator when one or both of the operands are of the type of your class or structure.
For more information, see.
Types of Operator Procedure
An operator procedure can be one of the following types:
A definition of a unary operator where the argument is of the type of your class or structure.
A definition of a binary operator where at least one of the arguments is of the type of your class or structure.
A definition of a conversion operator where the argument is of the type of your class or structure.
A definition of a conversion operator that returns the type of your class or structure.
Conversion operators are always unary, and you always use CType as the operator you are defining.
The syntax for declaring an operator procedure is as follows:
Public Shared [Widening | Narrowing] Operator operatorsymbol ( operand1 [, operand2 ]) As datatype
' Statements of the operator procedure.
You use the Widening or Narrowing keyword only on a type conversion operator. The operator symbol is alwaysfor a type conversion operator.
You declare two operands to define a binary operator, and you declare one operand to define a unary operator, including a type conversion operator. All operands must be declared ByVal.
You declare each operand the same way you declare parameters for.
Because you are defining an operator on a class or structure you have defined, at least one of the operands must be of the data type of that class or structure. For a type conversion operator, either the operand or the return type must be of the data type of the class or structure.
For more details, see.
You invoke an operator procedure implicitly by using the operator symbol in an expression. You supply the operands the same way you do for predefined operators.
The syntax for an implicit call to an operator procedure is as follows:
Dim testStruct As structurename
Dim testNewStruct As structurename = testStruct operatorsymbol 10
Illustration of Declaration and Call
The following structure stores a signed 128-bit integer value as the constituent high-order and low-order parts. It defines the + operator to add two veryLong values and generate a resulting veryLong value.
Public Structure veryLong Dim highOrder As Long Dim lowOrder As Long Public Shared Operator +(ByVal v As veryLong, _ ByVal w As veryLong) As veryLong Dim sum As New veryLong sum = v Try sum.lowOrder += w.lowOrder Catch ex As System.OverflowException sum.lowOrder -= (Long.MaxValue - w.lowOrder + 1) sum.highOrder += 1 End Try sum.highOrder += w.highOrder Return sum End Operator End Structure
The following example shows a typical call to the + operator defined on veryLong.