Operator 陳述式Operator 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. 表示此運算子程式會定義擴展轉換。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 除非 On``Option Strict,否則為選擇性。Optional unless Option Strict is On. 這個運算元的資料類型。Data type of this operand.

type
除非 On``Option Strict,否則為選擇性。Optional 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

您只能在類別或結構中使用 OperatorYou 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. 您不能為任一運算元指定 ByRefOptionalParamArrayYou 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
BinaryBinary +, -, *, /, \, &, ^, >>, <<, =, <>, >, >=, <, <=, 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時,您必須指定 [Widening] 或 [Narrowing]。When 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.

  • 如果您定義 <<>> 運算子,它們必須同時為 operand2operandtype 指定 Integer 類型。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. 例如,即使兩個運算元都不 Boolean,比較運算子(例如 =<> 也可以傳回 BooleanFor example, a comparison operator such as = or <> can return Boolean even if neither operand is Boolean.

邏輯運算子和位元運算子Logical and Bitwise Operators

AndOrNotXor 運算子可以在 Visual Basic 中執行邏輯 or 位運算。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.

您不能直接使用 Operator 語句來定義 AndAlso 運算子。You cannot define the AndAlso operator directly with an Operator statement. 不過,如果您已完成下列條件,您可以使用 AndAlsoHowever, you can use AndAlso if you have fulfilled the following conditions:

  • 您已在要用於 AndAlso的相同運算元類型上定義 AndYou 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.

  • 您已在定義 And的類別或結構上定義 IsFalse 運算子。You have defined the IsFalse operator on the class or structure on which you have defined And.

同樣地,如果您已在相同的運算元上定義 Or、使用類別或結構的傳回型別,而且您已在類別或結構上定義 IsTrue,則可以使用 OrElseSimilarly, 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:

  • 它必須一律傳回 type類型的有效值。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.

如果轉換程式可能不會成功,或可能造成未處理的例外狀況,您必須將它宣告為 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 語句來定義結構的外框,其中包含 AndOrIsFalseIsTrue 運算子的運算子程式。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 的兩個運算元,並 abc傳回型別。And 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. 這些定義可讓呼叫程式碼使用 AndAndAlsoOrOrElse,並搭配 abc類型的運算元。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

另請參閱See also