Operator Statement


Operator Statement

Syntax

     Public [Overloads] Shared [Shadows] [Widening | Narrowing] _           Operator symbol (operand1[, operand2]) [As type]        [statements]        Return expression        [statements]     End Function 


Overloads (optional)

Indicates that more than one declaration of this overloaded operator exists, each with a different argument signature.


Shadows (optional)

Indicates that the overloaded operator shadows an identically named element in a base class.


Widening (optional)

Indicates that the use of the overloaded operator will never result in the loss of data due to a change in data types.


Narrowing (optional)

Indicates that the use of the overloaded operator may possibly result in the loss of data, due to a change in data types.


symbol (required)

The operator to overload. One of the following operator symbols:

  • Unary: +, -, IsFalse, IsTrue, Not, CType

  • Binary: +, -, *, /, \, &, ^, >>, <<, = (comparison), <>, >, >=, <, <=, And, Like, Mod, Or, Xor


operand1 (required; any)

The first operand used with the operator. For binary operators, this operand appears to the left of the operator. It has the form:

     [ByVal] operandName As operandType 

where operandName is the name of the operand as used in the operator procedure, and operandType is the data type as it is used in the operator.


operand2 (optional; any)

The second operand used with the binary operator. This operand appears to the right of the operator. It has the form:

     [ByVal] operandName As operandType 

where operandName is the name of the operand as used in the operator procedure, and operandType is the data type as it is used in the operator. Its data type may or may not match the data type of operand1 or of the return type.


type (optional; Type)

The return data type of the operator. This may or may not match the data type of the operand(s).


statements (optional)

Program code to be executed within the procedure.


expression (optional)

The value of type type to return from the procedure to the calling procedure as a result of the operation.

Description

New in 2005. The Operator statement defines an overloaded operator. Normally, the standard Visual Basic operators can only be used with the built-in data types as defined by the VB compiler. The Operator statement allows you to define new uses for these standard operators.

Operators are fully described, with examples, in the "Operator Overloading" section of Chapter 5.

Usage at a Glance

  • All overloaded operators are Public and Shared. They can be used without a specific instance of the type.

  • At least one of the operands must be of the type that contains the overloaded operator definition. When overloading the CType operator, either the operand or the return value must be of that containing data type.

  • Certain pairs of operators must be overloaded as a matched set:

    • = and <>

    • > and <

    • >= and <=

    • IsTrue and IsFalse

  • The IsTrue and IsFalse operators must return data of type Boolean.

  • The << and >> operators must use Integer for the data type of operand2.

  • The And, Or, Not, and Xor operators overload the bitwise versions of these operators, not the logical versions.

  • The AndAlso operator requires that And and IsFalse also be overloaded. Likewise, the OrElse operator requires that Or and IsTrue also be overloaded.

Version Differences

Operator overloading is new in Visual Basic 2005.

See Also

Sub Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net