Name

CType Function

Named Arguments

No

Syntax

CType(expression, typename)
expression

Use: Required

Data Type: Any

The data item to be converted

typename

Use: Required

Type: Keyword

The data type, object type, structure, or interface to which expression is to be converted

Return Value

expression cast as a typename interface, object, structure, or data type.

Description

Converts any expression that can be interpreted as an object to Object.

Rules at a Glance

  • expression can be any data, object, structure, or interface type.

  • typename can be any data type (such as Boolean, Byte, Decimal, Long, Short, String, etc.), structure type, object type, or interface that can be used with the As clause in a Dim statement.

  • If the function fails, or if the converted value of expression is outside the range allowed by typename, an InvalidCastException exception occurs.

Example

Option Strict On Imports Microsoft.VisualBasic Imports System Interface IEmployee Property Name( ) As String Property Salary( ) As Decimal End Interface Public Class CSalaried Implements IEmployee Dim sName As String Dim decSalary AS DEcimal Public Property Name( ) As String Implements IEmployee.Name Get Name = sName End Get Set sName = Value End Set End Property Public Property Salary( ) As Decimal Implements IEmployee.Salary Get Salary = decSalary End Get Set decSalary = Value End Set End Property End Class Module modMain Public Sub Main( ) Dim oSal As New CSalaried Dim oSal2 As CSalaried Dim oEmp As IEmployee oSal.Name = "John Doe" ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.