Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Reply
Use TryParse instead of Parse
Old 02-21-2007, 06:04 PM Use TryParse instead of Parse
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
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 offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-21-2007, 10:25 PM Re: Use TryParse instead of Parse
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
This must be a .NET thing. I've never heard of Parse or TryParse before.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-22-2007, 04:00 PM Re: Use TryParse instead of Parse
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
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 offline
Reply With Quote
View Public Profile
 
Old 02-24-2007, 07:25 AM Re: Use TryParse instead of Parse
vivekar's Avatar
Webmaster Talker

Posts: 612
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
(Active since 2003) |
Please login or register to view this content. Registration is FREE
vivekar is offline
Reply With Quote
View Public Profile Visit vivekar's homepage!
 
Old 02-26-2007, 04:06 PM Re: Use TryParse instead of Parse
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Interesting, I haven't used Convert.xxx - I'll have to try this. Thanks for the tip!
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Use TryParse instead of Parse
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.17300 seconds with 12 queries