運算子程序 (Visual Basic)Operator Procedures (Visual Basic)

運算子程式是一系列的 Visual Basic 語句,可在您定義的類別或結構上定義標準運算子的行為(例如 *<>And)。An operator procedure is a series of Visual Basic statements that define the behavior of a standard operator (such as *, <>, or And) on a class or structure you have defined. 這也稱為「運算子多載」。This is also called operator overloading.

定義運算子程式的時機When to Define Operator Procedures

當您已定義類別或結構時,您可以將變數宣告為該類別或結構的類型。When you have defined a class or structure, you can declare variables to be of the type of that class or structure. 有時候這類變數需要參與作業做為運算式的一部分。Sometimes such a variable needs to participate in an operation as part of an expression. 若要這樣做,它必須是運算子的運算元。To do this, it must be an operand of an operator.

Visual Basic 只會定義其基本資料類型的運算子。Visual Basic defines operators only on its fundamental data types. 當一或兩個運算元屬於您的類別或結構類型時,您可以定義運算子的行為。You can define the behavior of an operator when one or both of the operands are of the type of your class or structure.

如需詳細資訊,請參閱Operator 語句For more information, see Operator Statement.

運算子程式的類型Types of Operator Procedure

運算子程式可以是下列其中一種類型:An operator procedure can be one of the following types:

  • 一元運算子的定義,其中引數是您的類別或結構的類型。A definition of a unary operator where the argument is of the type of your class or structure.

  • 二元運算子的定義,其中至少有一個引數屬於您的類別或結構的類型。A definition of a binary operator where at least one of the arguments is of the type of your class or structure.

  • 轉換運算子的定義,其中的引數是您的類別或結構的類型。A definition of a conversion operator where the argument is of the type of your class or structure.

  • 轉換運算子的定義,可傳回您的類別或結構的類型。A definition of a conversion operator that returns the type of your class or structure.

轉換運算子一律是一元,而且您一律會使用 CType 做為您所定義的運算子。Conversion operators are always unary, and you always use CType as the operator you are defining.

宣告語法Declaration Syntax

宣告運算子程式的語法如下:The syntax for declaring an operator procedure is as follows:

Public Shared [Widening | Narrowing] Operator operatorsymbol ( operand1 [,  operand2 ]) As datatype

' Statements of the operator procedure.

End Operator

您只會在類型轉換運算子上使用 WideningNarrowing 關鍵字。You use the Widening or Narrowing keyword only on a type conversion operator. 類型轉換運算子的運算子符號一律是CType 函數The operator symbol is always CType Function for a type conversion operator.

您可以宣告兩個運算元來定義二元運算子,並宣告一個運算元來定義一元運算子,包括型別轉換運算子。You declare two operands to define a binary operator, and you declare one operand to define a unary operator, including a type conversion operator. 所有運算元都必須宣告 ByValAll operands must be declared ByVal.

您可以用宣告Sub 程式參數的相同方式來宣告每個運算元。You declare each operand the same way you declare parameters for Sub Procedures.

資料類型Data Type

因為您是在已定義的類別或結構上定義運算子,所以至少有一個運算元必須是該類別或結構的資料類型。Because you are defining an operator on a class or structure you have defined, at least one of the operands must be of the data type of that class or structure. 若為類型轉換運算子,運算元或傳回類型必須是類別或結構的資料類型。For a type conversion operator, either the operand or the return type must be of the data type of the class or structure.

如需詳細資訊,請參閱Operator 語句For more details, see Operator Statement.

呼叫語法Calling Syntax

您可以在運算式中使用運算子符號,以隱含的方式叫用運算子程式。You invoke an operator procedure implicitly by using the operator symbol in an expression. 提供運算元的方式與預先定義的運算子相同。You supply the operands the same way you do for predefined operators.

對運算子程式進行隱含呼叫的語法如下:The syntax for an implicit call to an operator procedure is as follows:

Dim testStruct As 結構名稱Dim testStruct As structurename

Dim testNewStruct As 結構名稱 = testStruct 運算子符號 10Dim testNewStruct As structurename = testStruct operatorsymbol 10

宣告和呼叫的圖例Illustration of Declaration and Call

下列結構會將帶正負號的128位整數值儲存為組成高順序和低序位部分。The following structure stores a signed 128-bit integer value as the constituent high-order and low-order parts. 它會定義 + 運算子,以加入兩個 veryLong 值,並產生產生的 veryLong 值。It defines the + operator to add two veryLong values and generate a resulting veryLong value.

Public Structure veryLong
    Dim highOrder As Long
    Dim lowOrder As Long
    Public Shared Operator +(ByVal v As veryLong, 
                             ByVal w As veryLong) As veryLong
        Dim sum As New veryLong
        sum = v
        Try
            sum.lowOrder += w.lowOrder
        Catch ex As System.OverflowException
            sum.lowOrder -= (Long.MaxValue - w.lowOrder + 1)
            sum.highOrder += 1
        End Try
        sum.highOrder += w.highOrder
        Return sum
    End Operator
End Structure

下列範例顯示 veryLong上所定義之 + 運算子的一般呼叫。The following example shows a typical call to the + operator defined on veryLong.

Dim v1, v2, v3 As veryLong
v1.highOrder = 1
v1.lowOrder = Long.MaxValue
v2.highOrder = 0
v2.lowOrder = 4
v3 = v1 + v2

請參閱See also