简体   繁体   中英

How to shorten if-else-if statement in c#

Can someone shorten this code? There are 14 Buttons and 8 text Boxes. The action will be perform if the textbox is not empty and if it is not empty, then when you click it the button corresponding with the letter in the textbox will be visible again making the textbox empty.

 private void txt1_Click(object sender, EventArgs e)
 {
        if (txt1.Text == "J")
        {
            txt1.Text = "";
            btn1.Visible = true;
        }
        else if (txt1.Text == "M")
        {
            txt1.Text = "";
            btn2.Visible = true;
        }
        else if (txt1.Text == "Y")
        {
            txt1.Text = "";
            btn3.Visible = true;
        }
        else if (txt1.Text == "E")
        {
            if (btn4.Visible == true)
            {
                txt1.Text = "";
                btn5.Visible = true;
            }
            else
            {
                txt1.Text = "";
                btn4.Visible = true;
            }
        }
        else if (txt1.Text == "Q")
        {
            txt1.Text = "";
            btn6.Visible = true;
        }
        else if (txt1.Text == "L")
        {
            if (btn7.Visible == true)
            {
                txt1.Text = "";
                btn10.Visible = true;
            }
            else
            {
                txt1.Text = "";
                btn7.Visible = true;
            }
        }
        else if (txt1.Text == "B")
        {
            txt1.Text = "";
            btn8.Visible = true;
        }
        else if (txt1.Text == "C")
        {
            txt1.Text = "";
            btn9.Visible = true;
        }
        else if (txt1.Text == "P")
        {
            txt1.Text = "";
            btn11.Visible = true;
        }
        else if (txt1.Text == "I")
        {
            txt1.Text = "";
            btn12.Visible = true;
        }
        else if (txt1.Text == "K")
        {
            txt1.Text = "";
            btn13.Visible = true;
        }
        else if (txt1.Text == "O")
        {
            txt1.Text = "";
            btn14.Visible = true;
        }
}

Assuming all the btn variables are part of the state of your class then you can declare a method like so:

public Button Click(String txt) {
    switch(txt) {
        case "J":
            return btn1;
        case "M":
            return btn2;
        case "Y":
            return btn3;
        case "E":
            return (btn4.Visible ? btn5 : btn4);
        case "Q":
            return btn6;
        case "L":
            return (btn7.Visible ? btn10 : btn7);
        case "B":
            return btn8;
        case "C":
            return btn9;
        case "P":
            return btn11;
        case "I":
            return btn12;
        case "K":
            return btn13;
        case "O":
            return btn14;
    }
    return null;
}

and then you call it:

var button = Click(txt1.Text);
if(button != null) {
    button.Visible = true;
    txt1.Text = "";
}

If however the btn variables have local scope, than instead of a method you can just define an inline Func<String,Button> delegate as so:

Func<String, Button> Click = txt => {
    switch(txt) {
        ...
    }
};

You'd still have to handle the special cases ( "E" and "L" ) but you could use a Dictionary<string, Button> which would allow you to do a lookup:

var buttonDictionary = new Dictionary<string, Button>();
buttonDictionary["J"] = btn1;
buttonDictionary["M"] = btn2;
//etc...

if (buttonDictionary.Keys.Contains(txt1.Text))
{
    txt1.Text = "";
    buttonDictionary[txt1.Text].Visible = false;
}

That would reduce most of the repetitive code.

Looking at this: I'd suggest you go back think of more logical way to do whatever you're doing.

Out of my experience, any enormous chains of if-else in code represent that there has been a logical issue somewhere.

Also, familiarize yourself with switch statements

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