walkerofskies1 walkerofskies1 - 1 month ago 12
C# Question

String was not recognized as a valid boolean C#

I get the error "String was not recognized as a valid boolean" I am coding in C# and can't seems to find out what the problem is. Any insight would be very much appreciated. This is my code where it's failing:

public static SalesInfo Parse(string stringValue)
string[] words;
SalesInfo salesInfo = new SalesInfo();

words = StringMethods.ParseCsvString(stringValue.Trim());
salesInfo.ID = int.Parse(words[0]);
salesInfo.Name = words[1];
salesInfo.City = words[2];
salesInfo.Senior = bool.Parse(words[3]);<----Error here
salesInfo.Veteran = bool.Parse(words[4]);
salesInfo.PurDate = Date.Parse(words[5]);
salesInfo.ItemPrice = Double.Parse(words[6]);
salesInfo.Quantity = int.Parse(words[7]);

return salesInfo;


bool.Parse will only parse a string of "True" or "False" (case-insensitive).

The MSDN documentation for bool.TryParse shows a good example of the types of strings that can be parsed.

If your input string is some other variation of "truthiness" you would have to write your own parser. Something like:

public static SalesInfo Parse(string stringValue)
    ...cut for brevity...

    salesInfo.Senior = ParseBool(words[3]);

    return salesInfo;

public bool ParseBool(string input)
    switch (input.ToLower())
        case "y":
        case "yes":
            return true;
            return false;