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. 請參閱ShadowsSee Shadows.

Widening
除非您指定所需的轉換運算子NarrowingRequired for a conversion operator unless you specify Narrowing. 表示定義這個運算子程序Widening轉換。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. 表示定義這個運算子程序Narrowing轉換。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 StrictOnOptional unless Option Strict is On. 此運算元資料類型。Data type of this operand.

type
選擇性除非Option StrictOnOptional 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. 您無法指定ByRefOptional,或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.

以這種方式定義的運算子會呼叫運算子多載無論您使用Overloads關鍵字。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
一元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類型operandtypeoperand2If 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

AndOrNot,和Xor運算子可以執行在 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直接與運算子Operator陳述式。You 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擴展轉換一定會成功在執行階段,而縮小轉換可以在執行階段失敗。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,和IsTrue運算子。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. AndOr各自採用兩個運算元的型別abc並傳回型別abcAnd and Or each take two operands of type abc and return type abc. IsFalseIsTrue各自採用單一類型的運算元abc,並傳回BooleanIsFalse and IsTrue each take a single operand of type abc and return Boolean. 這些定義允許呼叫的程式碼,以使用AndAndAlsoOr,以及OrElse類型的運算元使用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