Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.
[ <attrlist> ] Public [ Overloads ] Shared [ Shadows ] [ Widening | Narrowing ] Operator operatorsymbol ( operand1 [, operand2 ]) [ As [ <attrlist> ] type ] [ statements ] [ statements ] Return returnvalue [ statements ] End Operator
You can use Operator only in a class or structure. This means the declaration context for an operator cannot be a source file, namespace, module, interface, procedure, or block. For more information, see Declaration Contexts and Default Access Levels (Visual Basic).
All operators must be Public Shared. You cannot specify ByRef, Optional, or ParamArray for either operand.
You cannot use the operator symbol or identifier to hold a return value. You must use the Return statement, and it must specify a value. Any number of Return statements can appear anywhere in the procedure.
Defining an operator in this way is called operator overloading, whether or not you use the Overloads keyword. The following table lists the operators you can define.
+, -, IsFalse, IsTrue, Not
+, -, *, /, \, &, ^, >>, <<, =, <>, >, >=, <, <=, And, Like, Mod, Or, Xor
Note that the = operator in the binary list is the comparison operator, not the assignment operator.
When you define CType, you must specify either Widening or Narrowing.
You must define certain operators as matched pairs. If you define either operator of such a pair, you must define the other as well. The matched pairs are the following:
= and <>
> and <
>= and <=
IsTrue and IsFalse
Data Type Restrictions
Every operator you define must involve the class or structure on which you define it. This means that the class or structure must appear as the data type of the following:
The operand of a unary operator.
At least one of the operands of a binary operator.
Either the operand or the return type of a conversion operator.
Certain operators have additional data type restrictions, as follows:
If you define the IsTrue and IsFalse operators, they must both return the Boolean type.
If you define the << and >> operators, they must both specify the Integer type for the operandtype of operand2.
The return type does not have to correspond to the type of either operand. For example, a comparison operator such as = or <> can return Boolean even if neither operand is Boolean.
Logical and Bitwise Operators
The And, Or, Not, and Xor operators can perform either logical or bitwise operations in Visual Basic. However, if you define one of these operators on a class or structure, you can define only its bitwise operation.
You cannot define the AndAlso operator directly with an Operator statement. However, you can use AndAlso if you have fulfilled the following conditions:
You have defined And on the same operand types you want to use for AndAlso.
Your definition of And returns the same type as the class or structure on which you have defined it.
You have defined the IsFalse operator on the class or structure on which you have defined And.
Similarly, you can use OrElse if you have defined Or on the same operands, with the return type of the class or structure, and you have defined IsTrue on the class or structure.
Widening and Narrowing Conversions
A widening conversion always succeeds at run time, while a narrowing conversion can fail at run time. For more information, see Widening and Narrowing Conversions (Visual Basic).
If you declare a conversion procedure to be Widening, your procedure code must not generate any failures. This means the following:
It must always return a valid value of type type.
It must handle all possible exceptions and other error conditions.
It must handle any error returns from any procedures it calls.
If there is any possibility that a conversion procedure might not succeed, or that it might cause an unhandled exception, you must declare it to be Narrowing.
The following code example uses the Operator statement to define the outline of a structure that includes operator procedures for the And, Or, IsFalse, and IsTrue operators. And and Or each take two operands of type abc and return type abc. IsFalse and IsTrue each take a single operand of type abc and return Boolean. These definitions allow the calling code to use And, AndAlso, Or, and OrElse with operands of type abc.
Public Structure abc Dim d As Date Public Shared Operator And(ByVal x As abc, ByVal y As abc) As abc Dim r As New abc ' Insert code to calculate And of x and y. Return r End Operator Public Shared Operator Or(ByVal x As abc, ByVal y As abc) As abc Dim r As New abc ' Insert code to calculate Or of x and y. Return r End Operator Public Shared Operator IsFalse(ByVal z As abc) As Boolean Dim b As Boolean ' Insert code to calculate IsFalse of z. Return b End Operator Public Shared Operator IsTrue(ByVal z As abc) As Boolean Dim b As Boolean ' Insert code to calculate IsTrue of z. Return b End Operator End Structure