简体   繁体   中英

String was not recognized as a valid boolean

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.

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.

Try This:

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

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

    bool result = message.ToLower().Equals("true");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM