Learn to program in Visual Basic .NET, the right way

Conversions - TryParse

TryParse Methods

The most effective method to convert data is to use the data type's TryParse method.

The TryParse method will attempt to convert a value to a designated data type, and return a Boolean value based on if the conversion was a success.

The TryParse method has two parameters:

  • Input
  • Conversion

The input is the value to be converted and the conversion is the variable that will store the converted value if the conversion was a success.

The following demonstrates how to use the TryParse method:

Fiddle: Live Demo

Exception

If the value to be converted fails the conversion, a FormatException will be thrown.

Remarks

The TryParse method should be used if there is any ambiguity as to whether or not the value to be converted will successfully be converted.