tags:

views:

688

answers:

4

Why was it decided that when parsing a boolean, 0/1 are not acceptable?

When parsing any integer type value, it accepts numerical strings to be parsed. (And if .NET can parse the string "One hundred million two hundred and sixty five thousand eight hundred and sixty five" I would be surprised).

What makes booleans special? They are essentially 0 as false, and non-zero as true in my experience...

Is there a bcl method to parse a string like this, and if not, why?

Note: I forgot to specify in a string "0" and "1". Curious though that if already an int it works as I anticipated. Maybe this caused the confusion.

+2  A: 

What makes booleans special? They are essentially 0 as false, and non-zero as true in my experience...

That is an implementation detail, and isn't at all relevant.

true is a boolean value. false is a boolean value. Anything else is not.

If you want to parse something such that the string "0" evaluates false while anything else evaluates true, you can use:

!mystr.Equals("0");
Anon.
A: 

You want Convert.ToBoolean(int value) not sure what's up with the Parse methods :-)

Code for no useful purpose:

        int falseInt = 0;
        int trueInt = 1;

        bool falseBool;
        bool trueBool;

        if (bool.TryParse(falseInt.ToString(), out falseBool))
        {
            if (!falseBool)
            {
                MessageBox.Show("TryParse: False");
            }
        }

        if (bool.TryParse(trueInt.ToString(), out trueBool))
        {
            if (!trueBool)
            {
                MessageBox.Show("TryParse: True");
            }
        }

        falseBool = Convert.ToBoolean(falseInt);
        trueBool = Convert.ToBoolean(trueInt);

        if (!falseBool)
        {
            MessageBox.Show("Convert: False");
        }

        if (trueBool)
        {
            MessageBox.Show("Convert: True");
        }
Cory Charlton
+9  A: 

0 and (not-zero) are not equal to "false" and "true", they're just the representation chosen by C. Other languages use 0 for true and -1 for false, or other schemes entirely. A boolean is not a 0 or a 1, it's a true or a false.

Should it also handle "yes" and "no", "off" and "on", and all of the myriad other things that are analogous to booleans? Where would you draw the line?

Aric TenEyck
Thanks! This is what I was looking for, an explanation of why.If some languages indeed use 0 for true, and -1 for false, I honestly think they are screwed up, but that is a great reason not to automatically assume through a parse that some developers may use blindly.
Aequitarum Custos
A: 

Unfortunately, this happens a lot in .NET. For example, I can't remember if it's XML Serializer or XmlConvert but one of them fails if the casing of True/False are not correct.

You can round trip through integer to get what you want.

string s = "2";
int i = Convert.ToInt32(s);
bool b = Convert.ToBoolean(i);

In the above case, anything non-zero will evaluate to true.

For this reason, I created a class I use all over called ConversionStrategy which takes into account the source type and destination type and chooses the most ideal (and flexible) conversion strategy for making the conversion.

Josh Einstein