How well do you know your TryParse()?
-
What would you expect
foo
to be after this code fragment runs? Post your answer without first peeking at the docs! :-Dstring badNumericString = "Bogus";
Decimal foo = Decimal.MinValue;
bool status = Decimal.TryParse (badNumericString, out foo); // status is false, as expectedAnswer:
Decimal.MinValue
(since the parse failed)- Something else (if so, what?)
/ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
I think foo would be zero, but that's just a guess. I think Int32.TryParse and Double.TryParse set the out parameter to zero if the parse fails, but I don't recall. If TryParse fails, I usually set the value to some known default that I can handle - I don't care what TryParse sets it to, especially is MS decides to change it down the road.
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
-
I think foo would be zero, but that's just a guess. I think Int32.TryParse and Double.TryParse set the out parameter to zero if the parse fails, but I don't recall. If TryParse fails, I usually set the value to some known default that I can handle - I don't care what TryParse sets it to, especially is MS decides to change it down the road.
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
You're absolutely correct.
dybs wrote:
If TryParse fails, I usually set the value to some known default that I can handle
I agree that's the advisable thing to do. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com