CType Function

   
CType Function

Named Arguments

No

Syntax

 CType(   expression   ,   typename   ) 
expression (required; any)

The data item to be converted

typename (required; 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 an expression to the specified data type if possible; otherwise , returns an error.

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.

  • When Option Strict is set to On, then implicit data type conversions can only be widening; that is, implicit data type conversion only occurs from smaller data types to "wider" data types, such as from Integer to Long. In this case, to perform a narrowing type conversion, we can use CType . For instance, if Option Strict is On , the following code produces an error:

     Dim iInteger As Integer = 1 Dim lLong As Long = 2 iInteger = lLong On the other hand, the following code is fine: Dim iInteger As Integer = 1 Dim lLong As Long = 2 iInteger = Ctype(lLong, Integer) 

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" oSal.Salary = 30000 console.writeline(oSal.Name) oEmp = CType(oSal, IEmployee) console.writeline(oEmp.Name) oSal2 = CType(oEmp, CSalaried) console.writeline(oSal2.name) End Sub End Module 

Programming Tips and Gotchas

  • CType can perform the same conversions as the individual conversion functions and raises a runtime error if it is asked to perform a conversion that an individual conversion function cannot perform. For example, in the function call:

     bVal = CType(   Var1   , Boolean) 

    Var1 can be any numeric value, any numeric string, or a string whose value is either "True" or "False" .

  • Like most of the conversion functions, CType is not actually a function in the Microsoft.VisualBasic namespace. Instead, it is similar to a Visual C++ macro; the compiler translates the function call into inline code.

  • In part, CType is a "convenience function" that provides the functionality of the entire set of conversion functions in a single function. Its real significance, however, comes when you want to convert a derived object to the type of its base class, or when you want to convert an object to the type of its virtual base class (that is, its interface).

  • Upcasting a derived object to its parent object type can be done implicitly. However, downcasting back from the base class type to the derived object type cannot be done implicitly if Option Strict is On. Instead, CType can be used to perform this conversion. This is illustrated in the example.

VB.NET/VB 6 Differences

The CType function is new to VB.NET.

See Also

CBool Function, CByte Function, CChar Function, CDate Function, CDbl Function, CDec Function, CInt Function, CLng Function, CObj Function, CShort Function, CSng Function, CStr Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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