1

If I declared a bool isTrue = false; // init it to false

and I can get the value from a string strVal = T; // I assumed it is the TRUE value

I heard it is not a good code style to compare string in C# like

if (isTrue.tostring() == strVal) {}.

Some time, I covert the the string variable to enum then I can compare it more convenient.

Is there any good method to do it?

1
  • 1
    still typo? no need to do "strVal.tostring()" Commented Jul 5, 2010 at 8:45

8 Answers 8

7

Yes, you parse the string into a boolean first.

Try this:

bool someBool = false; 

string boolVal = "true";
bool stringBool;
bool.TryParse(boolVal, out stringBool);

if (someBool == boolVal) 
{

}

Alternatively to handle 'T' and 'F' try these methods:

public bool ParseString(string maybeBool)
{
    return ParseString(maybeBool, false);
}

public bool ParseString(string maybeBool, bool def)
{
    bool stringBool;
    if (bool.TryParse(maybeBool, out stringBool))
        return stringBool;

    if (string.Equals(maybeBool, "T", StringComparison.OrdinalIgnoreCase))
        return true;

    if (string.Equals(maybeBool, "F", StringComparison.OrdinalIgnoreCase))
        return false;

    return def;
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Graphain. I debugged and found it doesn't work when the boolVal = "T" not "true"; Then I can get stringBool = false after TryParse() How can I cover the problem with your method? Thanks.
No TryParse doesn't handle "T" it will return false (i.e failed to parse) in this case. Check its return value and if TryParse returns false do some custom parsing to handle "T", "F" etc... You should maybe throw an exception if boolVal still doesn't match anything.
The new example will not compile because 'default' is a keyword.
@mjf196 okay so rename the variable and I'll do the same on the example, thanks
3

Try bool.Parse() method instead.

1 Comment

or bool.TryParse is even better if you aren't sure that the input string is a valid bool value.
1
bool.Parse(boolVal) == isTrue 

Comments

1

There is no need to convert boolean values to strings in order to compare the two. You can simply compare the two boolean values directly:

if (isTrue == boolVal) {}

Update: (following updated question)

You can parse a string into a boolean and use the resulting boolean in your comparison (as above), using either bool.Parse or bool.TryParse.

1 Comment

Sorry for my original post above. Acturally the boolVal is a ` strVal` - T. I take is as a bool value TRUE.
1

you may compare boolean type instead.

bool temp = bool.Parse(strVal);

if(isTrue == temp)

Comments

1

Yet another version which i use a lot is simply Convert.ToBoolean(stringFromBoolVal)

regards

Comments

0

If you really want to do string comparisons:

if (string.Equals(isTrue.ToString(), strValue)) { }

1 Comment

add .ToLower() on both sides then! ;)
0

How about trying this simple way to determine the value of bool variable isTrue:

isTrue = strVal == "T";

isTrue will return true if the strVal is equaled to "T". And return false if not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.