Procedimentos de operador (Visual Basic)
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 Instrução Operator.
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] Operatoroperatorsymbol (operando1 [, operando2]) 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 Função CType (Visual Basic) 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 Subprocedimentos (Visual Basic).
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 Instrução Operator.
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 = testStruct operatorsymbol 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 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
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
For more information and examples, see Operator Overloading in Visual Basic 2005.
Consulte também
Tarefas
Como: Definir um operador (Visual Basic)
Como: Definir um operador de conversão (Visual Basic)
Como: Chamar um procedimento de operador (Visual Basic)
Como: Usar uma classe que define operadores (Visual Basic)
Referência
Conceitos
Subprocedimentos (Visual Basic)
Procedimentos de função (Visual Basic)