Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i make this code more optimized

Tags:

c#

.net

asp.net

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;
    }
}
like image 464
Nick Kahn Avatar asked Aug 03 '10 14:08

Nick Kahn


People also ask

How do I make code more optimized?

Optimize Program Algorithm For any code, you should always allocate some time to think the right algorithm to use. So, the first task is to select and improve the algorithm which will be frequently used in the code. 2. Avoid Type Conversion Whenever possible, plan to use the same type of variables for processing.

What does it mean to Optimise code?

We say that code optimization is writing or rewriting code so a program uses the least possible memory or disk space, minimizes its CPU time or network bandwidth, or makes the best use of additional cores. In practice, we sometimes default to another definition: Writing less code.


3 Answers

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.

like image 78
djdd87 Avatar answered Oct 13 '22 15:10

djdd87


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;
like image 9
NullUserException Avatar answered Oct 13 '22 16:10

NullUserException


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

            }
like image 5
azram19 Avatar answered Oct 13 '22 14:10

azram19