我怎样才能使这个代码更加优化

时间:2010-08-03 14:00:29

标签: c# .net asp.net

我该如何优化此代码?我不喜欢有案例陈述,有没有办法可以改进这段代码?

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

6 个答案:

答案 0 :(得分:33)

够容易。根据案例选项,您只能看到一个项目可见,因此只需按如下方式设置可见性:

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行代码用于实际可见性设置。

答案 1 :(得分:10)

另一种方式:

// set everything to false   
Dictionary<string, type> d = new Dictionary<string, type>()
{
    {"date", pnlDate},
    {"subject", plnSubject},
    {"officer", plnOfficer},
    {"status", plnStatus}
};

d[selVal].Visible = true;

答案 2 :(得分:5)

protected void ddlFilterResultBy_SelectedIndexChanged(object sender, EventArgs e)
            { 
                string selVal = ddlFilterResultBy.SelectedValue.ToString().ToLower();
                        pnlDate.Visible = false;
                        pnlSubject.Visible = false;
                        pnlofficer.Visible = false;
                        pnlCIA.Visible = false;
                        pnlMedia.Visible = false;
                        pnlStatus.Visible = false;
                switch (selVal)
                {
                    case "date":
                        pnlDate.Visible = true;                    
                        break;

                    case "subject":
                        pnlSubject.Visible = true;
                        break;

                    case "officer":
                        pnlofficer.Visible = true;
                        break; 
                    case "status":
                        pnlStatus.Visible = true;
                        break;  
                }

            }

答案 3 :(得分:3)

你可以这样做:

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

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

或者这个虽然不太可读,但会更准确:

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

  pnlDate.Visible = String.Equals(selVal, "date", StringComparison.OrdinalIgnoreCase);
  pnlSubject.Visible = String.Equals(selVal, "subject", StringComparison.OrdinalIgnoreCase);
  pnlofficer.Visible = String.Equals(selVal, "officer", StringComparison.OrdinalIgnoreCase);
  pnlCIA.Visible = String.Equals(selVal, "cia", StringComparison.OrdinalIgnoreCase);
  pnlMedia.Visible = String.Equals(selVal, "media", StringComparison.OrdinalIgnoreCase);
  pnlStatus.Visible = String.Equals(selVal, "status", StringComparison.OrdinalIgnoreCase);
}

答案 4 :(得分:2)

优化和提高可读性之间存在差异。 所以我想你正在考虑提高可读性,因为这里并不真正需要优化。这里没有算法可以调整,以便在相当长的时间内加快速度。

答案1和2将是我提高可读性的选择

答案 5 :(得分:0)

我认为你需要 tabcontrol ..

如果您不喜欢这些标签,请隐藏标签,并通过代码隐藏页面索引。

优点是您可以在设计时编辑和查看GUI。

维护起来会容易得多。