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 - 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
-
-
-
-
-
-
-
-
-
(Locked)
Understanding strings6m 41s
-
(Locked)
Create string variables4m
-
(Locked)
Concatenation and interpolation2m 56s
-
(Locked)
Search string contents2m 59s
-
(Locked)
Modify string contents3m 3s
-
(Locked)
String creation with StringBuilder5m 39s
-
(Locked)
Compare and equals5m 51s
-
(Locked)
Convert and format numbers to strings8m 4s
-
(Locked)
Implicit covert strings to numbers2m 50s
-
(Locked)
Use Parse and TryParse conversions5m 4s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-