String was not recognized as a valid boolean

Cheetah picture Cheetah · Jan 24, 2013 · Viewed 19.4k times · Source

I am sending the string representation of a boolean through a socket and reading it the other end.

void Send(bool value)
{
    Socket.Send(value.ToString());
}

void Receive()
{
    string message = Socket.Receive();

    Console.WriteLine(message) // Always equal to either 'True' or 'False (without quotes)

    bool result = bool.Parse(message) // here I get the format exception.
}

but I get the following exception when I try and parse my message:

String was not recognized as a valid boolean.

The value when I get the exception is: True. With NO whitespace.

Answer

B.K. picture B.K. · May 30, 2015

At first glance, I would consider it having an issue with untrimmed space..., but that's not the case, as Boolean.Parse uses TryParse and that, in turn, trims the space in one of its attempts:

public static Boolean Parse (String value) {
    if (value==null) throw new ArgumentNullException("value");
    Contract.EndContractBlock();
    Boolean result = false;
    if (!TryParse(value, out result)) {
        throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));            
    }
    else {
        return result;
    }
}

public static Boolean TryParse (String value, out Boolean result) {
    result = false;
    if (value==null) {
        return false;
    }
    // For perf reasons, let's first see if they're equal, then do the
    // trim to get rid of white space, and check again.
    if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
        result = true;
        return true;
    }
    if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
        result = false;
        return true;
    }

    // Special case: Trim whitespace as well as null characters.
    value = TrimWhiteSpaceAndNull(value);

    if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
        result = true;
        return true;
    }

    if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
        result = false;
        return true;
    }

    return false;
}

Reference: http://referencesource.microsoft.com/#mscorlib/system/boolean.cs,e2a8f2e50ecf93c0,references

So, there must be something else going on. Perhaps there is an issue with the format, UTF-8, ANSI, ASCII, etc. One of your requirements is that you want a bool, so that you won't have two cases for True and False, so why not do something like this:

bool result = message.ToLower().Contains("true"); // true or false

EDIT:

After reading some of the comments, it seems you're expecting cases beyond True or False, in which case the result could be invalid. I suggest something like this:

var lMessage = message.ToLower();
bool? result = lMessage.Equals("true") ? true : lMessage.Equals("false") ? false : null;

So, if the message contains True, it's true; if False, it's false; otherwise, it's null, which indicates an invalid message. You can then check to see if result is null and either display the invalid message or do something else. I'm not sure what your routine is from there on.