How well do you know your TryParse()?
-
Do we actually care, if the value isn't being used?
string badNumericString = "Bogus";
Decimal foo; // will be initialized to 0 by the runtime
if (!Decimal.TryParse (badNumericString, out foo)) foo = 1; // default value here..and if the TryParse don't use the value, it'll probably initialize it with the same value it initializes an empty variable. Since the variable hasn't been set (according to application logic), we can't be sure about the value unless we explicitly set it. (Future versions of .NET might display other behavior)
Bastard Programmer from Hell :suss:
Eddy Vluggen wrote:
it'll probably initialize it with the same value it initializes an empty variable
Double checked using Reflector...
public static bool TryParse(string s, out decimal result)
{
return Number.TryParseDecimal(s, NumberStyles.Number, NumberFormatInfo.CurrentInfo, out result);
}internal static unsafe bool TryParseDecimal(string value, NumberStyles options, NumberFormatInfo numfmt, out decimal result)
{
byte* stackBuffer = stackalloc byte[0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
result = 0M; // result is set to zero here!
if (!TryStringToNumber(value, options, ref number, numfmt, true))
{
return false;
}
if (!NumberBufferToDecimal(number.PackForNative(), ref result))
{
return false;
}
return true;
}... so it WILL be zero.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Well it's zero, right?
That is what the doc[^] says. This entire thread seams to suggest no-one reads or beliefs it. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
That is what the doc[^] says. This entire thread seams to suggest no-one reads or beliefs it. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
no-one reads or beliefs it
Maybe I'm a one-percenter. :-D My understanding is that all TryParse methods are expected to set the value to
zero
/null
/default(T)
when they fail -- so that's what I do when I write a Tryxxx method.public bool TryParse ( string Name , out T Value ) { bool result = true ; Value = default(T) ;
public static bool
TryGetValue<T>
(
this object Source
,
out T Value
)
{
bool result = false ;Value = default(T) ;
-
I would argue that forgetting to set a valid initial value is programmer error, however I now like the idea of an explicit default. That's what I initially implemented (link[^]), but like Alan, switched to the 2 parameter
ref
version. I'm going to switch back. Thanks, /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
Then I prefer to provide a value; something like this:
public virtual T ExecuteScalar<T> ( T IfNull ) {
you could do similar for TryParse.
-
Do we actually care, if the value isn't being used?
string badNumericString = "Bogus";
Decimal foo; // will be initialized to 0 by the runtime
if (!Decimal.TryParse (badNumericString, out foo)) foo = 1; // default value here..and if the TryParse don't use the value, it'll probably initialize it with the same value it initializes an empty variable. Since the variable hasn't been set (according to application logic), we can't be sure about the value unless we explicitly set it. (Future versions of .NET might display other behavior)
Bastard Programmer from Hell :suss:
Eddy Vluggen wrote:
Do we actually care
Not particularly, but we care whether or not it does what the documentation says.
-
Luc Pattyn wrote:
no-one reads or beliefs it
Maybe I'm a one-percenter. :-D My understanding is that all TryParse methods are expected to set the value to
zero
/null
/default(T)
when they fail -- so that's what I do when I write a Tryxxx method.public bool TryParse ( string Name , out T Value ) { bool result = true ; Value = default(T) ;
public static bool
TryGetValue<T>
(
this object Source
,
out T Value
)
{
bool result = false ;Value = default(T) ;
I agree. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Well it's zero, right?
Yessir, it is. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Do we actually care, if the value isn't being used?
string badNumericString = "Bogus";
Decimal foo; // will be initialized to 0 by the runtime
if (!Decimal.TryParse (badNumericString, out foo)) foo = 1; // default value here..and if the TryParse don't use the value, it'll probably initialize it with the same value it initializes an empty variable. Since the variable hasn't been set (according to application logic), we can't be sure about the value unless we explicitly set it. (Future versions of .NET might display other behavior)
Bastard Programmer from Hell :suss:
Eddy Vluggen wrote:
Do we actually care, if the value isn't being used?
It is being used. The example I gave was intentionally limited to focus on the question. In my app,
foo
(not its real name) is used after theTryParse()
executes. Different things happen depending on whetherfoo
isDecimal.MinValue
. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
That is what the doc[^] says. This entire thread seams to suggest no-one reads or beliefs it. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
You are right in assuming I didn't read the doc. :) /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
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