CType Function


CType Function

Syntax

     Dim result As typename = CType(expression, typename) 


expression (required; any)

The value to be converted. This can be any data, object, structure, or interface type.


typename (required)

The data type, object type, structure, or interface to which expression is to be converted. This can be virtually anything that can appear after the As clause of a Dim statement.

Description

The CType function converts an expression or object to the specified type.

Usage at a Glance

  • If expression cannot be converted to the new data type (perhaps due to incompatibility of the types), an error occurs.

  • CType can perform the same conversions as the individual conversion functions. For example, the last two lines in this code are equivalent:

         Dim booleanString As String = "True"     Dim realBoolean As Boolean     realBoolean = CBool(booleanString)     realBoolean = CType(booleanString, Boolean) 

  • This function does not support named arguments.

  • CType is often used to convert an object between its derived and base types. It is also used to restore an object back to its true type from an instance of type Object.

  • Assignment of a derived object to its parent object type can be done implicitly. However, assignments in the opposite direction (from base to descendant) need to be cast with CType if Option Strict is On.

Example

Each list item added to a Windows Forms ListBox control includes an object of any type that is used to both display the text and store custom user data. Internally, it is stored as Object. When you retrieve the data object for a single list entry, you must convert it to the original type before using its members.

This example defines a simple form with a ListBox. When an item is selected, the data associated with the selected item is cast back to a TeamDetails object.

     Public Class BaseballTeams        Inherits System.Windows.Forms.Form        ' ----- Define a simple class to store in the list.        Protected Class TeamDetails           Public TeamName As String           Public Members As Integer           Public Sub New(ByVal fullName As String, _                 ByVal totalMembers As Integer)              ' ----- Simple constructor.              TeamName = fullName              Members = totalMembers           End Sub           Public Overrides Function ToString(  ) As String              ' ----- Properly displays information in list box.              Return TeamName & " (" & Members & ")"           End Function        End Class        Private Sub BaseballTeams_Load(ByVal sender As System.Object, _              ByVal e As System.EventArgs) Handles MyBase.Load           ' ----- Add some basic teams.           TeamNames.Items.Add(New TeamDetails("Tokyo Giants", 18))           TeamNames.Items.Add(New TeamDetails("Seattle Mariners", 20))        End Sub        Private Sub TeamNames_SelectedIndexChanged( _              ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles TeamNames.SelectedIndexChanged           ' ----- To display the details, we must convert the type.           Dim selectedTeam As TeamDetails           selectedTeam = CType(TeamNames.SelectedItem, TeamDetails)           MsgBox("Team = " & selectedTeam.TeamName & vbCrLf & _              "Members = " & selectedTeam.Members)        End Sub     End Class 

Version Differences

  • The CType function was not present in VB 6.

  • Visual Basic 2005 adds features that let you define custom CType conversions for use with your own types. These features are part of the operator overloading additions, and they are described in Chapter 5.

See Also

CBool Function, CByte Function, CChar Function, CDate Function, CDbl Function, CDec Function, CInt Function, CLng Function, CObj Function, CSByte Function, CShort Function, CSng Function, CStr Function, CUInt Function, CULong Function, CUShort Function




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