简体   繁体   English

如何在C#中缩短if-else-if语句

[英]How to shorten if-else-if statement in c#

Can someone shorten this code? 有人可以缩短此代码吗? There are 14 Buttons and 8 text Boxes. 有14个按钮和8个文本框。 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: 假设所有btn变量都是类状态的一部分,则可以这样声明一个方法:

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: 但是,如果btn变量具有局部作用域,则可以使用以下方式定义内联Func<String,Button>委托,而不是方法:

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: 您仍然必须处理特殊情况( "E""L" ),但是您可以使用Dictionary<string, Button>来进行查找:

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. 根据我的经验,代码中if-else的巨大链条都表示某个地方存在逻辑问题。

Also, familiarize yourself with switch statements 另外,请熟悉switch语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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