Convert.ToBoolean fails with “0” value

后端 未结 8 2226
清歌不尽
清歌不尽 2020-12-14 16:37

I\'m trying to convert the value \"0\" ( System.String ) to its Boolean representation, like:

var myValue = Convert.To         


        
相关标签:
8条回答
  • 2020-12-14 17:40

    This is happening because Convert.ToBoolean is expecting one of the following:

    • "True" (String) = true
    • "False" (String) = false
    • 0 (numerical type; int, double, float, etc.) = false
    • Any non-0 (numerical type; ...) = true
    • null = false

    Any other value is invalid for Boolean.

    You've already got a clean approach:

    var myValue = Convert.ToBoolean(Convert.ToInt32("0"));
    

    Edit: You can create an extension method that will handle a few of these cases for you, while hiding away the ugliness of handling the conversion.

    This extension provides a very loose interpretation of Boolean:

    • "True" (String) = true
    • "False" (String) = false
    • "0" (String) = false
    • Any other string = true

    Code:

    public static class Extensions
    {
        public static Boolean ToBoolean(this string str)
        {
            String cleanValue = (str ?? "").Trim();
            if (String.Equals(cleanValue, "False", StringComparison.OrdinalIgnoreCase))
                return false;
            return
                (String.Equals(cleanValue, "True", StringComparison.OrdinalIgnoreCase)) ||
                (cleanValue != "0");
        }
    }
    

    Alternatively, if you want a more strict approach, which follows what the .NET Framework expects; then simply use try/catch statements:

    public static class Extensions
    {
        public static Boolean ToBoolean(this string str)
        {
            try
            {
                return Convert.ToBoolean(str);
            }
            catch { }
            try
            {
                return Convert.ToBoolean(Convert.ToInt32(str));
            }
            catch { }
            return false;
        }
    }
    

    Albeit, not a clean or pretty approach, but it guarantees more possibilities of getting the correct value. And, the Extensions class is tucked away from your data/business code.

    In the end, your conversion code is relatively simple to use:

    String myString = "1";
    Boolean myBoolean = myString.ToBoolean();
    
    0 讨论(0)
  • 2020-12-14 17:41
    public static class BooleanParser
    {
        public static bool SafeParse(string value)
        {
            var s = (value ?? "").Trim().ToLower();
            return s == "true" || s == "1";
        }
    }
    

    static readonly HashSet<string> _booleanTrueStrings = new HashSet<string> { "true", "yes", "1" };
    static readonly HashSet<string> _booleanFalseStrings = new HashSet<string> { "false", "no", "0" };
    
    public static bool ToBoolean(string value)
    {
        var v = value?.ToLower()?.Trim() ?? "";
        if (_booleanTrueStrings.Contains(v)) return true;
        if (_booleanFalseStrings.Contains(v)) return false;
        throw new ArgumentException("Unexpected Boolean Format");
    }
    
    0 讨论(0)
提交回复
热议问题