Click here to Skip to main content
13,005,192 members (62,353 online)
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads


35 bookmarked
Posted 8 Apr 2007

DirectCast vs. CType

, 8 Apr 2007
When to use DirectCast vs. CType.
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
            End If
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ButtonDirectCastR = New System.Windows.Forms.Button
        Me.ButtonCTypeR = New System.Windows.Forms.Button
        Me.ButtonCTypeV = New System.Windows.Forms.Button
        Me.ButtonDirectCastV = New System.Windows.Forms.Button
        Me.ButtonDirectCastR.Location = New System.Drawing.Point(12, 12)
        Me.ButtonDirectCastR.Name = "ButtonDirectCastR"
        Me.ButtonDirectCastR.Size = New System.Drawing.Size(153, 23)
        Me.ButtonDirectCastR.TabIndex = 0
        Me.ButtonDirectCastR.Text = "DirectCast on Reference Type"
        Me.ButtonDirectCastR.UseVisualStyleBackColor = True
        Me.ButtonCTypeR.Location = New System.Drawing.Point(12, 41)
        Me.ButtonCTypeR.Name = "ButtonCTypeR"
        Me.ButtonCTypeR.Size = New System.Drawing.Size(153, 23)
        Me.ButtonCTypeR.TabIndex = 1
        Me.ButtonCTypeR.Text = "CType on Reference Type"
        Me.ButtonCTypeR.UseVisualStyleBackColor = True
        Me.ButtonCTypeV.Location = New System.Drawing.Point(12, 99)
        Me.ButtonCTypeV.Name = "ButtonCTypeV"
        Me.ButtonCTypeV.Size = New System.Drawing.Size(153, 23)
        Me.ButtonCTypeV.TabIndex = 3
        Me.ButtonCTypeV.Text = "CType on Value Type"
        Me.ButtonCTypeV.UseVisualStyleBackColor = True
        Me.ButtonDirectCastV.Location = New System.Drawing.Point(12, 70)
        Me.ButtonDirectCastV.Name = "ButtonDirectCastV"
        Me.ButtonDirectCastV.Size = New System.Drawing.Size(153, 23)
        Me.ButtonDirectCastV.TabIndex = 2
        Me.ButtonDirectCastV.Text = "DirectCast on Value Type"
        Me.ButtonDirectCastV.UseVisualStyleBackColor = True
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(182, 137)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub
    Friend WithEvents ButtonDirectCastR As System.Windows.Forms.Button
    Friend WithEvents ButtonCTypeR As System.Windows.Forms.Button
    Friend WithEvents ButtonCTypeV As System.Windows.Forms.Button
    Friend WithEvents ButtonDirectCastV As System.Windows.Forms.Button

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tim Greenfield
Software Developer (Senior)
United States United States
I am a software architect and engineer who has been working in the industry since 1994. Although I officially started writing software many years earlier at age 11 by authoring games, graphics demos, or whatever I found interesting. I even landed my first consulting job at age 16 implementing a newly invented mathimatical algorithm for a team of researchers at the local University.

I've designed and implemented hundreds if not thousands of applications: client, web, and hybrid of all scales and was a contributing author to the Black Belt programming column of Visual Basic Programmer's Journal.

My goals when coding are always to write clean, reusable and optimized code using the latest and technology, tools and techniques.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 8 Apr 2007
Article Copyright 2007 by Tim Greenfield
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid