tags:

views:

76

answers:

4

Im playing around with TryParse()

But lets say the parsing fails, then returns false, and ... nothing.. Is there a way to get info back about what failed the parsing?

I saw something like that at codeproject, but i didnt really understand it.

Thanks :)

+5  A: 

No, there's no way of getting that information from the normal .NET routines. You could check for a few things manually:

  • Try parsing the number as a decimal. If that works, but parsing as an integer doesn't, then it's either out of the range of the integer, or it's not an integer.
  • Look for non-decimal, non +/-, non-decimal-point characters
  • Check whether it's an empty string

You haven't said what you're trying to parse (integer, double etc) or what options you want (allow hex, thousands separators etc) which makes it harder to give a good list of things to check.

Jon Skeet
Focus isnt set on something particular, so your answer is pretty good :)Thanks
Moulde
+2  A: 

The TryParse() method is there when you want to be shielded from any exceptions.

If you want to see the exceptions then why not use the standard Parse() method in a try/catch block which will allow you to view any FormatExceptions etc thrown? As expected, with exception handling, this could impact performance however if the Parse() is expected to succeed then this should be tolerable.

Andy Rose
+1  A: 

Why not just use the regular Parse method instead?

Svish
A: 

Very true..

Thanks for taking time to answer my question :)

Moulde