Use TryCast in Visual Basic .NET 2005

Provided by Dave Juth, Senior Systems Architect

Use the TryCast keyword in Visual Basic .NET 2005 to simplify casting operations. This is a new VB.NET language feature that is similar to the C# as operator. For example, without TryCast, you might do this to get a value from a collection into a string variable:

  Dim someValue As String

 

  If myCollection.Items(0) <> Nothing Then

    someValue = CType(myCollection.Items(0), String)

  Else

    someValue = String.Empty

  End If

 

With TryCast, you can simplify this as follows:

  Dim someValue As String = TryCast(myCollection.Items(0), String)


 


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS