tags:

views:

126

answers:

7

Hello.

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  A: 

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.

Oded
Sorry for my original post above. Acturally the `boolVal` is a ` strVal` - `T`. I take is as a `bool` value `TRUE`.
Nano HE
+4  A: 

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;
}
Graphain
@all. Thank you.
Nano HE
@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.
Nano HE
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.
Bear Monkey
See my update Nano HE to handle T and F
Graphain
The new example will not compile because 'default' is a keyword.
Bear Monkey
@mjf196 okay so rename the variable and I'll do the same on the example, thanks
Graphain
+2  A: 

Try bool.Parse() method instead.

sashaeve
or bool.TryParse is even better if you aren't sure that the input string is a valid bool value.
munissor
+1  A: 
bool.Parse(boolVal) == isTrue 
Al Bundy
+1  A: 

you may compare boolean type instead.

bool temp = bool.Parse(strVal);

if(isTrue == temp)
Arseny
A: 

If you really want to do string comparisons:

if (string.Equals(isTrue.ToString(), strValue)) { }
MrFox
it's case sensitive won't work if srtValue ="true" or "True"
Arseny
add .ToLower() on both sides then! ;)
Filip Ekberg
+1  A: 

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

regards

ReaperXmac