Reply
Use TryParse instead of Parse
Old 02-21-2007, 05:04 PM Use TryParse instead of Parse
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
The syntax might be a little unfamiliar to new programmers, but using TryParse instead of Parse can make your application faster and more scalable. This is because when it's unable to parse the supplied value, it falls back on a default value, instead of throwing an exception.
Learning Newbie is online now
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 02-21-2007, 09:25 PM Re: Use TryParse instead of Parse
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
This must be a .NET thing. I've never heard of Parse or TryParse before.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-22-2007, 03:00 PM Re: Use TryParse instead of Parse
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
Sorry, I was thinking the .NET forum was for WinForms and console applications, and that the ASP forum was for both flavors of ASP, so I put this here. But it turns out this forum is for Classic ASP, and I posted in the wrong place. So go ahead and ignore this message.

Although if you're curious, C# doesn't support CInt("413") so if you want to turn an ID=x query string into a number, you have to use int.Parse, or double.Parse, or what have you.

The problem is that if it can't parse the value ( say "four" ) it will throw an exception, which really slows things down. Most people don't know about TryParse, which performs the same work as type.Parse, except that it just returns a default value ( instead of throwing an exception ) when it can't read your string.
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 02-24-2007, 06:25 AM Re: Use TryParse instead of Parse
vivekar's Avatar
Webmaster Talker

Posts: 531
Instead of using int.Parse use Convert.ToInt32.

Convert.ToInt32 handles "null" better than int.Parse.

That's the difference between these two, though they do the same job.
vivekar is offline
Reply With Quote
View Public Profile Visit vivekar's homepage!
 
Old 02-26-2007, 03:06 PM Re: Use TryParse instead of Parse
Learning Newbie's Avatar
Moderator

Latest Blog Post:
What Does This Look Like?
Posts: 4,733
Name: John Alexander
Interesting, I haven't used Convert.xxx - I'll have to try this. Thanks for the tip!
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Use TryParse instead of Parse
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13839 seconds with 13 queries