Operator Statement
Operator Statement
Operator Statement
Visual Studio 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.
Syntax
[<attrlist>]Public[Overloads]Shared[Shadows][Widening|Narrowing]
Operatoroperatorsymbol(operand1[,operand2])[As[<attrlist>]type]
[statements]
[statements]
Returnreturnvalue
[statements]
EndOperator
Parts
attrlist
Optional. See Attribute List.
Public
Required. Indicates that this operator procedure has Public access.
Overloads
Optional. See Overloads.
https://msdn.microsoft.com/enus/library/hddt295a.aspx 1/7
1/25/2017 OperatorStatement
Shared
Required. Indicates that this operator procedure is a Shared procedure.
Shadows
Optional. See Shadows.
Widening
Required for a conversion operator unless you specify Narrowing. Indicates that this operator procedure defines a Widening conversion. See "Widening and Narrowing
Conversions" on this Help page.
Narrowing
Required for a conversion operator unless you specify Widening. Indicates that this operator procedure defines a Narrowing conversion. See "Widening and Narrowing
Conversions" on this Help page.
operatorsymbol
Required. The symbol or identifier of the operator that this operator procedure defines.
operand1
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.
operand2
Required for binary operators. The name and type of the right operand of a binary operator.
[ByVal]operandname[Asoperandtype]
Part Description
operandname Required. Name of the variable representing this operand. See Declared Element Names.
type
Optional unless OptionStrict is On. Data type of the value the operator procedure returns.
https://msdn.microsoft.com/enus/library/hddt295a.aspx 2/7
1/25/2017 OperatorStatement
statements
Optional. Block of statements that the operator procedure runs.
returnvalue
Required. The value that the operator procedure returns to the calling code.
EndOperator
Required. Terminates the definition of this operator procedure.
Remarks
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.
All operators must be PublicShared. 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.
Type Operators
Binary +, , *, /, \, &, ^, >>, <<, =, <>, >, >=, <, <=, 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.
Matched Pairs
https://msdn.microsoft.com/enus/library/hddt295a.aspx 3/7
1/25/2017 OperatorStatement
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 <>
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.
https://msdn.microsoft.com/enus/library/hddt295a.aspx 4/7
1/25/2017 OperatorStatement
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.
If you declare a conversion procedure to be Widening, your procedure code must not generate any failures. This means the following:
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.
Example
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.
VB
https://msdn.microsoft.com/enus/library/hddt295a.aspx 5/7
1/25/2017 OperatorStatement
PublicStructureabc
DimdAsDate
PublicSharedOperatorAnd(ByValxAsabc,ByValyAsabc)Asabc
DimrAsNewabc
'InsertcodetocalculateAndofxandy.
Returnr
EndOperator
PublicSharedOperatorOr(ByValxAsabc,ByValyAsabc)Asabc
DimrAsNewabc
'InsertcodetocalculateOrofxandy.
Returnr
EndOperator
PublicSharedOperatorIsFalse(ByValzAsabc)AsBoolean
DimbAsBoolean
'InsertcodetocalculateIsFalseofz.
Returnb
EndOperator
PublicSharedOperatorIsTrue(ByValzAsabc)AsBoolean
DimbAsBoolean
'InsertcodetocalculateIsTrueofz.
Returnb
EndOperator
EndStructure
See Also
IsFalse Operator
IsTrue Operator
Widening
Narrowing
Widening and Narrowing Conversions
Operator Procedures
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
https://msdn.microsoft.com/enus/library/hddt295a.aspx 6/7
1/25/2017 OperatorStatement
2017 Microsoft
https://msdn.microsoft.com/enus/library/hddt295a.aspx 7/7