Gentoo
Gentoo

Reputation: 347

FormatException 0 is not a valid value for Boolean

I am getting a formatexception with the following code. Any one know how to make BooleanConverter convert from 0/1 to true/false.

 bool bVal=true;
 string sVal = "0";
 Console.WriteLine(TypeDescriptor.GetConverter(bVal).ConvertFrom(sVal));

Thanks for the help!

Upvotes: 1

Views: 9126

Answers (6)

Chris
Chris

Reputation: 171

string _sOne = "1";
 string _sTrue = "TrUe";
 bool _bRandom = !Convert.ToBoolean(__sTrue);
 string _sResult = Convert.ToBoolean(_sOne);
 string _sResultB = Convert.ToBoolean(_sTrue);
 string _sResultC = _bRandom.ToString();

Upvotes: 0

code4life
code4life

Reputation: 15794

Your original code was good enough, only you can't set the string value to "0" and "1". In your code, TypeConverter.ConvertFrom() will eventually invoke Boolean.TryParse(), the code for which looks like this (thanks Reflector!)

public static bool TryParse(string value, out bool result)
{
    result = false;
    if (value != null)
    {
        if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
        {
            result = true;
            return true;
        }
        if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
        {
            result = false;
            return true;
        }
        if (m_trimmableChars == null)
        {
            char[] destinationArray = new char[string.WhitespaceChars.Length + 1];
            Array.Copy(string.WhitespaceChars, destinationArray, string.WhitespaceChars.Length);
            destinationArray[destinationArray.Length - 1] = '\0';
            m_trimmableChars = destinationArray;
        }
        value = value.Trim(m_trimmableChars);
        if ("True".Equals(value, StringComparison.OrdinalIgnoreCase))
        {
            result = true;
            return true;
        }
        if ("False".Equals(value, StringComparison.OrdinalIgnoreCase))
        {
            result = false;
            return true;
        }
    }
    return false;
}

So make the following change to your code and you should be good:

bool bVal = true;
string sVal = "false";
Console.WriteLine(TypeDescriptor.GetConverter(bVal).ConvertFrom(sVal));

Upvotes: 1

Patrick Cosyns
Patrick Cosyns

Reputation: 11

I just built a helper function that will handle the special case of booleans:

    Private Shared Function StringConverter(ByVal colValue As String, ByVal propertyType As Type) As Object

    Dim returnValue As Object

    'Convert the string value to the correct type
    Select Case propertyType
        Case GetType(Boolean)
            'Booleans need to be coded separately because by default only "True" and "False" will map to a Boolean
            'value. SQL Server will export XML with Booleans set to "0" and "1". We want to handle these without
            'changing the xml.
            Select Case colValue
                Case "0"
                    returnValue = False
                Case "1"
                    returnValue = True
                Case "False"
                    returnValue = False
                Case "True"
                    returnValue = True
                Case Else
                    Throw New ArgumentException("Value of '" & colValue & "' cannot be converted to type Boolean.")
            End Select
        Case Else
            'Let .Net Framework handle the conversion for us
            returnValue = TypeDescriptor.GetConverter(propertyType).ConvertFromString(colValue)

    End Select

    Return returnValue

End Function

Upvotes: 1

Ari Roth
Ari Roth

Reputation: 5534

If you're going to use Int32.Parse, use Int32.TryParse instead. It doesn't throw if the conversion fails, instead returning true or false. This means that it's more performant if all you're doing is checking to see if your input is a value. Example:

public static bool ConvertToBool(string value)
{
    int val = 0;
    return (int.TryParse(value, out val) && val == 0) ? false : true;
}

I have a tendency to go overboard on the ternary operator (x ? y : z), so here's a slightly easier-to-read version:

public static bool ConvertToBool(string value)
{
    int val = 0;
    if (int.TryParse(value, out val))
    {
        return val == 0 ? false : true;
    }

    return false;
}

(I tested them both. "1" returns true, "0" returns false.)

Upvotes: 2

JaredPar
JaredPar

Reputation: 755457

Try the following

public static bool ConvertToBasedOnIntValue(string value) {
  // error checking omitted for brevity
  var i = Int32.Parse(value);
  return value == 0 ? false : true;
}

Or you could use the following which won't throw exceptions but it will consider everything that is quite literally not 0 to be true

public static bool ConvertToBasedOnIntValue(string value) {
  if ( 0 == StringComparer.CompareOrdinal(value, "0") ) {
    return false;
  }
  return true;
}

Upvotes: 3

Jared Updike
Jared Updike

Reputation: 7277

There are only two cases so can just check for them explicitly.

Upvotes: 2

Related Questions