Operator StatementOperator Statement

声明在类或结构上定义运算符过程的运算符符号、操作数和代码。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 ]
Operator operatorsymbol ( operand1 [, operand2 ]) [ As [ <attrlist> ] type ]
    [ statements ]
    [ statements ]
    Return returnvalue
    [ statements ]
End Operator

组成部分Parts

attrlist
可选。Optional. 请参阅特性列表See Attribute List.

Public
必需。Required. 指示此运算符过程具有公共访问权限。Indicates that this operator procedure has Public access.

Overloads
可选。Optional. 请参阅重载See Overloads.

Shared
必需。Required. 指示此运算符过程是一个共享过程。Indicates that this operator procedure is a Shared procedure.

Shadows
可选。Optional. 请参阅阴影See Shadows.

Widening
除非指定,否则转换运算符是必需的 NarrowingRequired 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
除非指定,否则转换运算符是必需的 WideningRequired 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.

operand1operand2 具有以下语法和部分:operand1 and operand2 have the following syntax and parts:

[ ByVal ] operandname [ As operandtype ]

组成部分Part 说明Description
ByVal 可选,但传递机制必须是ByValOptional, but the passing mechanism must be ByVal.
operandname 必需。Required. 表示此操作数的变量的名称。Name of the variable representing this operand. 请参阅 Declared Element NamesSee Declared Element Names.
operandtype 除非 Option Strict 为,否则为可选 OnOptional unless Option Strict is On. 此操作数的数据类型。Data type of this operand.

type
除非 Option Strict 为,否则为可选 OnOptional unless Option Strict is On. 运算符过程返回的值的数据类型。Data type of the value the operator procedure returns.

statements
可选。Optional. 运算符过程运行的语句块。Block of statements that the operator procedure runs.

returnvalue
必需。Required. 运算符过程返回到调用代码的值。The value that the operator procedure returns to the calling code.

End OperatorEnd Operator
必需。Required. 终止此运算符过程的定义。Terminates the definition of this operator procedure.

备注Remarks

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.

所有运算符都必须是 Public SharedAll operators must be Public Shared. 不能 ByRef Optional ParamArray 为任一操作数指定、或。You cannot specify ByRef, Optional, or ParamArray for either operand.

不能使用运算符符号或标识符来保存返回值。You cannot use the operator symbol or identifier to hold a return value. 必须使用 Return 语句,并且必须指定一个值。You must use the Return statement, and it must specify a value. 任意数量的 Return 语句可以出现在过程中的任何位置。Any number of Return statements can appear anywhere in the procedure.

用这种方式定义运算符称为运算符重载,无论是否使用关键字都是如此 OverloadsDefining 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
一元Unary +, -, IsFalse, IsTrue, Not+, -, IsFalse, IsTrue, Not
二进制Binary +, -, *, /, \, &, ^, >>, <<, =, <>, >, >=, <, <=, And, Like, Mod, Or, Xor+, -, *, /, \, &, ^, >>, <<, =, <>, >, >=, <, <=, And, Like, Mod, Or, Xor
转换(一元)Conversion (unary) CType

请注意, = 二元列表中的运算符是比较运算符,而不是赋值运算符。Note that the = operator in the binary list is the comparison operator, not the assignment operator.

定义时 CType ,必须指定 WideningNarrowingWhen you define CType, you must specify either Widening or Narrowing.

匹配对Matched Pairs

必须将某些运算符定义为匹配对。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 <=

  • IsTrueIsFalseIsTrue 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:

  • 如果定义 IsTrueIsFalse 运算符,则它们必须返回 Boolean 类型。If you define the IsTrue and IsFalse operators, they must both return the Boolean type.

  • 如果定义 <<>> 运算符,则它们必须都为指定的 Integer 类型 operandtype operand2If 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. 例如,比较运算符(如或) = <> 可以返回, Boolean 即使两个操作数都不是 BooleanFor example, a comparison operator such as = or <> can return Boolean even if neither operand is Boolean.

逻辑运算符和位运算符Logical and Bitwise Operators

And、、 Or NotXor 运算符可在 Visual Basic 执行逻辑或按位运算。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.

不能 AndAlso 使用语句直接定义运算符 OperatorYou cannot define the AndAlso operator directly with an Operator statement. 但是, AndAlso 如果您满足以下条件,则可以使用:However, you can use AndAlso if you have fulfilled the following conditions:

  • 您定义了 And 要用于的相同操作数类型 AndAlsoYou have defined And on the same operand types you want to use for AndAlso.

  • 定义的 And 返回的类型与定义了它的类或结构的类型相同。Your definition of And returns the same type as the class or structure on which you have defined it.

  • 您已在已 IsFalse 定义的类或结构上定义运算符 AndYou have defined the IsFalse operator on the class or structure on which you have defined And.

同样, OrElse 如果你 Or 在相同的操作数上定义了类或结构的返回类型,并且已 IsTrue 在类或结构上定义,则可以使用。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 ConversionsWidening and Narrowing Conversions

在运行时,扩大转换始终会成功,而收缩转换可能会在运行时失败。A widening conversion always succeeds at run time, while a narrowing conversion can fail at run time. 有关详细信息,请参阅 Widening and Narrowing ConversionsFor more information, see Widening and Narrowing Conversions.

如果将转换过程声明为,则 Widening 过程代码不得生成任何失败。If you declare a conversion procedure to be Widening, your procedure code must not generate any failures. 这表示:This means the following:

  • 它必须始终返回类型的有效值 typeIt 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.

如果转换过程可能会失败或可能导致未经处理的异常,则必须将其声明为 NarrowingIf 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

下面的代码示例使用 Operator 语句来定义结构的轮廓,其中包括 AndOrIsFalse 和运算符的运算符过程 IsTrueThe 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``Or每个都采用类型和返回类型的两个操作数 abc abcAnd and Or each take two operands of type abc and return type abc. IsFalse``IsTrue每个都采用类型的单个操作数 abc 并返回 BooleanIsFalse and IsTrue each take a single operand of type abc and return Boolean. 这些定义允许调用代码将 AndAndAlsoOrOrElse 与类型的操作数一起使用 abcThese 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

另请参阅See also