繁体   English   中英

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

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

有人可以缩短此代码吗? 有14个按钮和8个文本框。 如果文本框不为空且不为空,则将执行该操作,然后单击该文本框时,与文本框中字母对应的按钮将再次可见,从而使文本框为空。

 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;
        }
}

假设所有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;
}

然后您将其称为:

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

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

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

您仍然必须处理特殊情况( "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;
}

这将减少大多数重复代码。

对此:建议您回去考虑更合理的方法来做您正在做的事情。

根据我的经验,代码中if-else的巨大链条都表示某个地方存在逻辑问题。

另外,请熟悉switch语句

暂无
暂无

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

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