次の方法で共有


Operator Procedures 

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 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.

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.

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

You use the Widening or Narrowing keyword only on a type conversion operator. 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. All operands must be declared ByVal.

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.

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 structurename

Dim testNewStruct As structurename= testStructoperatorsymbol 10

Illustration of Declaration and Call

The following structure stores a signed 128-bit integer value as the constituent high-order and low-order parts. It defines the + operator to add twoveryLongvalues and generate a resultingveryLongvalue.

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

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

Tasks

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

Reference

Operator Statement

Concepts

Procedures in Visual Basic
Sub Procedures
Function Procedures
Property Procedures
Procedure Parameters and Arguments