Feb
2
Written by:
Dylan Barber
2/2/2009 4:22 PM
Has anyone used this little bit of code. I dont' know about you but I had set a variable known value then used this fun bit of code to determine if another incoming variable is an integer and parse it to that - guess what it returns a zero (0) if the incoming variable isn't a integer. Very hard to troubleshoot!
Now I know people are going to tell me its documented in the MSDN site on page yada that this is the expected behaviour but who has time to read the manual when you are coding your pants off?
1 comment(s) so far...
Re: Integer.TryParse
I had to look this up, and of course its documented. But really it was DNN's "Null.NullInteger" that threw us off. .NET itself considers zero the default value for an integer. So if we hadn't got so used to assuming -1 is an unset value for an integer, this wouldn't have surprised us at all.
Even still, I think it should have left the variable being passed in alone and kept it at its previous value.
For example, we were passing in a -1 as the return integer, and when the function fails to parse the value it was changing it to a zero. This surprised us as we expected it to leave the value alone (stay as -1 )
By Lyle Boy on
2/8/2009 11:03 PM
|