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
Optional. See Attribute List.
Required. Indicates that this operator procedure has Public (Visual Basic) access.
Optional. See Overloads (Visual Basic).
Required. Indicates that this operator procedure is a Shared (Visual Basic) procedure.
Optional. See Shadows (Visual Basic).
Required for a conversion operator unless you specify Narrowing. Indicates that this operator procedure defines a Widening (Visual Basic) conversion. See "Widening and Narrowing Conversions" on this Help page.
Required for a conversion operator unless you specify Widening. Indicates that this operator procedure defines a Narrowing (Visual Basic) conversion. See "Widening and Narrowing Conversions" on this Help page.
Required. The symbol or identifier of the operator that this operator procedure defines.
Required. The name and type of the single operand of a unary operator (including a conversion operator) or the left operand of a binary operator.
Required for binary operators. The name and type of the right operand of a binary operator.
operand1 and operand2 have the following syntax and parts:
[ ByVal ] operandname [ As operandtype ]
Optional, but the passing mechanism must be ByVal (Visual Basic).
Required. Name of the variable representing this operand. See Declared Element Names (Visual Basic).
Optional unless Option Strict is On. Data type of this operand.
Optional unless Option Strict is On. Data type of the value the operator procedure returns.
Optional. Block of statements that the operator procedure runs.
Required. The value that the operator procedure returns to the calling code.
- End Operator
Required. Terminates the definition of this operator procedure.
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
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.
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.
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
IsFalse Operator (Visual Basic)
IsTrue Operator (Visual Basic)
Widening (Visual Basic)
Narrowing (Visual Basic)
Widening and Narrowing Conversions (Visual Basic)
Operator Procedures (Visual Basic)
How to: Define an Operator (Visual Basic)
How to: Define a Conversion Operator (Visual Basic)
How to: Call an Operator Procedure (Visual Basic)
How to: Use a Class that Defines Operators (Visual Basic)