| 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 |
|
||||||||
| ms.topic | article | ||||||||
| helpviewer_keywords |
|
||||||||
| ms.assetid | 0dce42cc-f0b0-4c14-9f62-018b21f33497 | ||||||||
| caps.latest.revision | 16 | ||||||||
| author | dotnet-bot | ||||||||
| ms.author | dotnetcontent |
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.
-
Use the operator symbol in an expression in the ordinary way.
-
Be sure the data types of the operands are appropriate for the operator, and in the correct order.
-
The operator contributes to the value of the expression as expected.
-
Use
CTypeinside an expression. -
Be sure the data types of the operands are appropriate for the conversion, and in the correct order.
-
CTypecalls the conversion operator procedure and returns the converted value.
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.
Be sure the class or structure you are using defines the operator you want to use.
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