TryCast Function


TryCast Function

Syntax

     Dim result As typename = tryCast(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. However, this type must have an inheritance relationship with the type of expression.

Description

New in 2005. The tryCast 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 relationship, that is, if one does not eventually derive from the other.

  • The cast may fail if it is a narrowing cast and Option Strict is On. Under such conditions, Nothing is returned.

  • The TRyCast function works like DirectCast, but it returns Nothing instead of generating an error on a narrowing failure.

  • This function does not support named arguments.

Version Differences

The TRyCast function is new to Visual Basic 2005.

See Also

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