tags:

views:

429

answers:

6

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;
    }
}
+31  A: 

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.

GenericTypeTea
I'd probably add parantheses to make it more readable though.
ray dey
+1: simple and clear
RedFilter
@ray dey - Fairy noof. Done.
GenericTypeTea
thanks .........
Abu Hamzah
I would remove the parantheses, because that's how StyleCop guides me to the light ;-)
Steven
on top of this, you could:turn off view state on both pnlCIA and pnlMedia, set visibility=false as default value in your aspx. Thus you can get rid of the 2 .visible=false lines.Essentially on every postback visible will always be set to false by default for both panel unless it is set to false.
ronaldwidha
+5  A: 
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;  
                }

            }
azram19
+10  A: 

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;
NullUserException
+1. Very nice..
Steven
+1 Good thought...
The King
What's with the downvote? I know the code doesn't do *exactly* the same thing as the original one; it's just to show the idea.
NullUserException
+2  A: 

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);
}
mattmc3
+1 thanks.......
Abu Hamzah
+2  A: 

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

Ashwani Roy
A: 

I think you need a tabcontrol here..

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.

It'll be a lot easier to maintain.

Wouter van Nifterick
@Wouter, i use jquery with div but i endup having problems you can see the question i posted here http://stackoverflow.com/questions/3367166/jquery-div-disappears-on-postback and i drop the idea of using jquery/div
Abu Hamzah