Use tryparse if that is possible or have a validation
I faced a number of error for parsing a string into int. In most of cases, there is a validation missing.
Code
If (IsNumeric(str)) | |
{ | |
int i =0; | |
Int32.TryParse(str,out i); | |
} |
You can use regex inside IsNumber or use foreach loop to check whether each character is numberic.
BUT NEVER NEVER DO LIKE THIS:
Code
try | |
{ | |
int i= Int32.Parse(i); | |
} | |
catch(FormatException) | |
{ | |
// Error Hanlding | |
} |
Try-catch is an expensive operation!!!!!!!!
Secondly, I like to use TryParse even in the case, the progam can tolrate invalid value. TryParse can allow the logic to continue execution. I hate some things like stack track on the screen.
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 1371 feedbacks awaiting moderation...
Form is loading...