gpt4 book ai didi

c# - 我怎样才能使这段代码更优化

转载 作者:太空狗 更新时间:2023-10-29 19:48:20 24 4
gpt4 key购买 nike

我如何优化这段代码?我不喜欢 case 语句,有什么方法可以改进这段代码吗?

protected void ddlFilterResultBy_SelectedIndexChanged(object sender, EventArgs e)
{
string selVal = ddlFilterResultBy.SelectedValue.ToString().ToLower();

switch (selVal)
{
case "date":
pnlDate.Visible = true;
pnlSubject.Visible = false;
pnlofficer.Visible = false;
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = false;
break;

case "subject":
pnlDate.Visible = false;
pnlSubject.Visible = true;
pnlofficer.Visible = false;
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = false;
break;

case "officer":
pnlDate.Visible = false;
pnlSubject.Visible = false;
pnlofficer.Visible = true;
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = false;
break;

case "status":
pnlDate.Visible = false;
pnlSubject.Visible = false;
pnlofficer.Visible = false;
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = true;
break;

default:
pnlDate.Visible = false;
pnlSubject.Visible = false;
pnlofficer.Visible = false;
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = false;
break;
}
}

最佳答案

很简单。根据大小写选项,您只能使一项可见,因此只需按如下方式设置可见性:

pnlDate.Visible = (selVal == "date"); 
pnlSubject.Visible = (selVal == "subject");
pnlofficer.Visible = (selVal == "officer");
pnlCIA.Visible = false;
pnlMedia.Visible = false;
pnlStatus.Visible = (selVal == "status");

这比将所有内容都设置为 visible = false; 然后只显示您需要的项目要好,因为这只包含 6 行代码以进行实际的可见性设置。

关于c# - 我怎样才能使这段代码更优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3397235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com