TryParse() Method

Sometimes a program requires not only numeric input but also Integer input. If we attempt to convert the contents of the Text property of a Textbox to an Integer and it contains a number with a decimal point the program will throw an error.

 

image of integer exception

 

The value passes the Numeric test but cannot be converted into an Integer.

 

We can use the TryParse( ) Method of a numeric data type to determine if a value can be converted into that data type. The TryParse method attempts to convert the value from the first argument and store the result in the second argument. If the argument passed to the Integer.TryParse method CAN be converted to an Integer it is,  the value is stored in the second argument, and the method returns TRUE.

Image of successfull TryParse

 

If the argument passed to the Integer.TryParse method CANNOT be converted to an Integer the value of the second argument remains unchanged, and the method returns FALSE.

 

Image of unsuccessfull TryParse