Operator Procedures (Visual Basic)
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
An operator procedure is a series of Visual Basic statements that define the behavior of a standard operator (such as
And) on a class or structure you have defined. This is also called operator overloading.
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 Operator Statement.
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:
[Widening | Narrowing]
]) As datatype
' Statements of the operator procedure.
You use the
Narrowing keyword only on a type conversion operator. The operator symbol is always CType Function for 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
You declare each operand the same way you declare parameters for Sub Procedures.
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 Operator Statement.
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
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
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
Dim v1, v2, v3 As veryLong v1.highOrder = 1 v1.lowOrder = Long.MaxValue v2.highOrder = 0 v2.lowOrder = 4 v3 = v1 + v2
For more information and examples, see Operator Overloading in Visual Basic 2005.
Procedure Parameters and Arguments
How to: Define an Operator
How to: Define a Conversion Operator
How to: Call an Operator Procedure
How to: Use a Class that Defines Operators