DirectCast Function


DirectCast Function

Syntax

     Dim result As typename = DirectCast(expression, typename) 


expression (required; any)

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


typename (required; Type)

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. However, this type must have an inheritance relationship with the type of expression.

Description

The DirectCast function converts an expression or object to the specified type. The original type of expression must have an inheritance relationship with the new type, or an error occurs.

Usage at a Glance

  • The cast will fail if the source and target data types do not have an inheritance relationshipthat is, one does not eventually derive from the other. The cast may also fail if it is a narrowing cast and Option Strict is On.

  • This function does not support named arguments.

Version Differences

  • The DirectCast function is new to VB under .NET.

  • Visual Basic 2005 includes a new tryCast function that includes slightly different functionality.

See Also

CType Function, TryCast 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