Skip to content

Latest commit

 

History

History
70 lines (54 loc) · 3.77 KB

File metadata and controls

70 lines (54 loc) · 3.77 KB
title How to: Call an Operator Procedure (Visual Basic)
ms.custom
ms.date 07/20/2015
ms.prod .net
ms.reviewer
ms.suite
ms.technology
devlang-visual-basic
ms.topic article
helpviewer_keywords
operator procedures [Visual Basic], calling
procedures [Visual Basic], operator
procedure calls [Visual Basic], operator overloading
syntax [Visual Basic], Operator procedures
operators [Visual Basic], overloading
return values [Visual Basic], Operator procedures
overloaded operators [Visual Basic], calling
operator overloading
ms.assetid 0dce42cc-f0b0-4c14-9f62-018b21f33497
caps.latest.revision 16
author dotnet-bot
ms.author dotnetcontent

How to: Call an Operator Procedure (Visual Basic)

You call an operator procedure by using the operator symbol in an expression. In the case of a conversion operator, you call the CType Function to convert a value from one data type to another.

You do not call operator procedures explicitly. You just use the operator, or the CType function, in an assignment statement or an expression, the same way you ordinarily use an operator. [!INCLUDEvbprvb] makes the call to the operator procedure.

Defining an operator on a class or structure is also called overloading the operator.

To call an operator procedure

  1. Use the operator symbol in an expression in the ordinary way.

  2. Be sure the data types of the operands are appropriate for the operator, and in the correct order.

  3. The operator contributes to the value of the expression as expected.

To call a conversion operator procedure

  1. Use CType inside an expression.

  2. Be sure the data types of the operands are appropriate for the conversion, and in the correct order.

  3. CType calls the conversion operator procedure and returns the converted value.

Example

The following example creates two xref:System.TimeSpan structures, adds them together, and stores the result in a third xref:System.TimeSpan structure. The xref:System.TimeSpan structure defines operator procedures to overload several standard operators.

[!code-vbVbVbcnProcedures#29]

Because xref:System.TimeSpan overloads the standard + operator, the previous example calls an operator procedure when it calculates the value of combinedSpan.

For an example of calling a conversation operator procedure, see How to: Use a Class that Defines Operators.

Compiling the Code

Be sure the class or structure you are using defines the operator you want to use.

See Also

Operator Procedures
How to: Define an Operator
How to: Define a Conversion Operator
Operator Statement
Widening
Narrowing
Structure Statement
How to: Declare a Structure
Implicit and Explicit Conversions
Widening and Narrowing Conversions