DirectCast Operator

DirectCast Operator (Visual Basic)


Introduces a type conversion operation based on inheritance or implementation.

DirectCast does not use the Visual Basic run-time helper routines for conversion, so it can provide somewhat better performance than CType when converting to and from data type Object.

You use the DirectCast keyword similar to the way you use the CType Function (Visual Basic) and the TryCast Operator (Visual Basic) keyword. You supply an expression as the first argument and a type to convert it to as the second argument. DirectCast requires an inheritance or implementation relationship between the data types of the two arguments. This means that one type must inherit from or implement the other.

DirectCast generates a compiler error if it detects that no inheritance or implementation relationship exists. But the lack of a compiler error does not guarantee a successful conversion. If the desired conversion is narrowing, it could fail at run time. If this happens, the runtime throws an InvalidCastException error.

A comparison of the type conversion keywords is as follows.


Data types

Argument relationship

Run-time failure

CType Function (Visual Basic)

Any data types

Widening or narrowing conversion must be defined between the two data types

Throws InvalidCastException


Any data types

One type must inherit from or implement the other type

Throws InvalidCastException

TryCast Operator (Visual Basic)

Reference types only

One type must inherit from or implement the other type

Returns Nothing (Visual Basic)

The following example demonstrates two uses of DirectCast, one that fails at run time and one that succeeds.

Imports System.Diagnostics
Imports System.ComponentModel

Namespace MyProcessSample
    Class MyProcess
        ' Opens the Internet Explorer application.
        Public Sub OpenApplication(myFavoritesPath As String)
            ' Start Internet Explorer. Defaults to the home page.

            ' Display the contents of the favorites folder in the browser.
        End Sub 'OpenApplication

        ' Opens urls and .html documents using Internet Explorer.
        Sub OpenWithArguments()
            ' url's are not considered documents. They can only be opened
            ' by passing them as arguments.
            Process.Start("IExplore.exe", "")

            ' Start a Web page using a browser associated with .html and .asp files.
            Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
            Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
        End Sub 'OpenWithArguments

        ' Uses the ProcessStartInfo class to start new processes,
        ' both in a minimized mode.
        Sub OpenWithStartInfo()
            Dim startInfo As New ProcessStartInfo("IExplore.exe")
            startInfo.WindowStyle = ProcessWindowStyle.Minimized


            startInfo.Arguments = ""

        End Sub 'OpenWithStartInfo

        Shared Sub Main()
            ' Get the path that stores favorite links.
            Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)

            Dim myProcess As New MyProcess()

        End Sub 'Main
    End Class 'MyProcess
End Namespace 'MyProcessSample

In the preceding example, the run-time type of q is Double. CType succeeds because Double can be converted to Integer. However, the first DirectCast fails at run time because the run-time type of Double has no inheritance relationship with Integer, even though a conversion exists. The second DirectCast succeeds because it converts from type Form to type Control, from which Form inherits.

© 2015 Microsoft