From the course: Visual Basic Essential Training

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Use Parse and TryParse conversions

Use Parse and TryParse conversions - Visual Basic Tutorial

From the course: Visual Basic Essential Training

Use Parse and TryParse conversions

- [Instructor] Implicit conversion uses the Visual Basic conversion methods like CDbl and CInt. As we've seen, these methods throw exceptions when the input string is invalid. There is an alternative. To explicitly convert strings, use either Parse or TryParse. Like implicit conversion, Parse and TryParse understand how to take string data and convert to a numeric type. Parse is straightforward, but strict, it expects the input to always be valid. If the input is invalid, it throws an exception. In other words, it has the same problem as implicit conversion if there is invalid data in the string. TryParse on the other hand doesn't throw exceptions. Instead, it safely attempts to parse the input and returns a false result if the input is invalid. This allows us to check whether the parse was successful. And if it wasn't, we can gracefully handle the error, for example, by prompting the user to enter a valid input or providing a default value. This makes it a much better choice when…

Contents