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

时间:2022-10-11 03:55:13

how can i optimized this code? i dont like to have case statement, is there a way i can improve this code?

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

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 个解决方案

#1


33  

Easy enough. You're only ever making one item visible depending on the case option, so just set the visibility as follows:

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

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

This is better than setting everything to visible = false; and then only showing the item you need as this contains it all into just 6 lines of code for the actual visibility setting.

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

#2


10  

Another way:

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

d[selVal].Visible = true;

#3


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

            }

#4


3  

You could do this:

你可以这样做:

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

Or this one, while less readable, would be more accurate:

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

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

#5


2  

There is a difference between optimization and improving readability. So I guess you are looking at improving readability more as optimization is not really needed here. There is no algo here which you can tune to make this faster by a significant time .

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

Answer 1 and 2 will be my choice of improving readability

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

#6


0  

I think you need a tabcontrol here..

我想你需要一个tabcontrol ..

Just hide the tabs if you don't like those, and page index via code.

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

The advantage is that you'll be able to edit and view the GUI at designtime.

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

It'll be a lot easier to maintain.

维护起来会容易得多。

#1


33  

Easy enough. You're only ever making one item visible depending on the case option, so just set the visibility as follows:

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

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

This is better than setting everything to visible = false; and then only showing the item you need as this contains it all into just 6 lines of code for the actual visibility setting.

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

#2


10  

Another way:

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

d[selVal].Visible = true;

#3


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

            }

#4


3  

You could do this:

你可以这样做:

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

Or this one, while less readable, would be more accurate:

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

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

#5


2  

There is a difference between optimization and improving readability. So I guess you are looking at improving readability more as optimization is not really needed here. There is no algo here which you can tune to make this faster by a significant time .

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

Answer 1 and 2 will be my choice of improving readability

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

#6


0  

I think you need a tabcontrol here..

我想你需要一个tabcontrol ..

Just hide the tabs if you don't like those, and page index via code.

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

The advantage is that you'll be able to edit and view the GUI at designtime.

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

It'll be a lot easier to maintain.

维护起来会容易得多。