Convert.ToBoolean and Boolean.Parse don't accept 0 and 1
Solution 1:
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?
Solution 2:
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");
Solution 3:
The shared FormatHelper
class shown below provides a simple solution using two variations of an overloaded method called StringToBoolean
.
FormatHelper.StringToBoolean(String value)
FormatHelper.StringToBoolean(String value, Boolean NullOrEmptyDefault)
Both variations provide a case-insentive string match
1) The normal convertion from string to boolean defaulting empty or null strings to false
The following examples will result in a boolean
value of false
:-
Boolean myBool = FormatHelper.StringToBoolean("");
Boolean myBool = FormatHelper.StringToBoolean("0");
Boolean myBool = FormatHelper.StringToBoolean("false");
Boolean myBool = FormatHelper.StringToBoolean("False");
Boolean myBool = FormatHelper.StringToBoolean("no");
Boolean myBool = FormatHelper.StringToBoolean("off");
All other string values will result in a Boolean
value of true
such as:-
Boolean myBool = FormatHelper.StringToBoolean("1");
Boolean myBool = FormatHelper.StringToBoolean("true");
Boolean myBool = FormatHelper.StringToBoolean("True");
Boolean myBool = FormatHelper.StringToBoolean("yes");
Boolean myBool = FormatHelper.StringToBoolean("xyz blah");
Note: Edit the value of BooleanStringOff
in the class below to include more (or less) values for false/off
2) Follows the same rules as 1) above but allows a default value of true
to be supplied as the 2nd argument to the conversion.
The default value is used when the String
value is empty or null
. This is useful if a missing string value needs to signify a true
state.
The following code example will return true
Boolean myBool = FormatHelper.StringToBoolean("",true);
The following code example will return false
Boolean myBool = FormatHelper.StringToBoolean("false",true);
This is the code for the FormatHelper
class
public class FormatHelper
{
public static Boolean StringToBoolean(String str)
{
return StringToBoolean(str, false);
}
public static Boolean StringToBoolean(String str, Boolean bDefault)
{
String[] BooleanStringOff = { "0", "off", "no" };
if (String.IsNullOrEmpty(str))
return bDefault;
else if(BooleanStringOff.Contains(str,StringComparer.InvariantCultureIgnoreCase))
return false;
Boolean result;
if (!Boolean.TryParse(str, out result))
result = true;
return result;
}
}
Solution 4:
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.
Solution 5:
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");
}