简体   繁体   中英

How to ask say “or” in an if-statement

private void caseTextBox_TextChanged(object sender, EventArgs e)
        {
            var CTcase = caseTextBox.Text;
            caseTextBox.CharacterCasing = CharacterCasing.Upper;

            if (CTcase == "THRUHOLE") 
            {
                displayLabel3.Text = "0";
            }
            else if (CTcase == "EIAA")
            {
                displayLabel3.Text = "1";
            }
            else if (CTcase == "EIAB")
            {
                displayLabel3.Text = "2";
            }
            else if (CTcase == "EIAC")
            {
                displayLabel3.Text = "3";
            }
            else if (CTcase == "EIAD")
            {
                displayLabel3.Text = "4";
            }
            else
            {
                displayLabel3.Text = "error";
            }
        }

In each statement there are 3 values that it could equal to display the correct number. But I don't want to write 15 if / else statements to make it work. I tried using || , | , & , and && . But I keep getting an error that says

Operator cannot be applied to operands of type 'bool' and 'string'

 private void caseTextBox_TextChanged(object sender, EventArgs e)
        {
            var CTcase = caseTextBox.Text;
            caseTextBox.CharacterCasing = CharacterCasing.Upper;

            if (CTcase == "THRUHOLE") 
            {
                displayLabel3.Text = "0";
            }
            else if (CTcase == "EIAA" || "1206")
            {
                displayLabel3.Text = "1";
            }

I'd rather eliminate all ifs (which look ugly when being in great number and complicated) and create a single dictionary instead:

Dictionary<String, String> labels = new Dictionary<String, String>() {
  {"THRUHOLE", "0"},
  {"EIAA", "1"},
  {"1206", "1"},
  {"EIAB", "2"},
  {"EIAC", "3"},
  {"EIAD", "4"},
};

...

String text;

if (labels.TryGetValue(CTcase, out text)) 
  displayLabel3.Text = text;
else
  displayLabel3.Text = "error";

There are two operators that will probably do the trick.

| is an or which will cause all statements in the if to be executed prior to evaluation.

|| is an or else which will cause the statements to be executed from left to right until a condition is met (at which point it stops testing further conditions).

In your case, you could do something like:

if (CTcase == "THRUHOLE" || CTcase == "some other value" || CTcase == "something else") 
{
    displayLabel3.Text = "0";
}

For example:

if (CTcase == "THRUHOLE" || CTcase == "something else") 
        {
            displayLabel3.Text = "0";
        }

But - for cases like this one - use enum. It's much more readable ! https://msdn.microsoft.com/pl-pl/library/sbbt4032.aspx

It sounds like you might be using || improperly. It has to be a full statement in itself.

such as:

if(CTcase == "THRUHOLE" || CTcase == "somethingelse")
{
    displaylabel3.Text = "0";
}

this should worki:

CTcase = caseTextBox.Text;

if (CTcase == "test" || CTcase  == "Test" || CTcase  == "TEST")
{

}

My way to check if there is just a "writing error" i would do the following:

CTcase = caseTextBox.Text.ToLower();

if (CTcase == "test")
{

}

But this only works if you don't want to make a differnce between "Test" and "test".

I think you can extend the method of string class like this

public static class StringExtension
{
    public static bool In(this string source, params string[] matches)
    {
        return matches.Contains(source);
    }
}

And then use it in your program, without worrying about to many if-clause

string displayLabel = string.Empty;
string CTcase = "EIAA";

if (CTcase.In("THRUHOLE"))
{
    displayLabel = "0";
}
else if (CTcase.In("EIAA", "1206"))
{
    displayLabel = "1";
}

I hope this may help you.

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