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;
}
}
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.
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.
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.
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;
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With