c# - how can i make this code more optimized -
How can I customize this code? I do not like taking case statement, is there a way I can improve this code?
protected ddlFilterResultBy_SelectedIndexChanged (object sender, EventArgs e) {string selVal = ddlFilterResultBy.SelectedValue.ToString (). Reducing(); 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; }}
Easy enough You can only make one item visible on the basis of case options , So just set the visibility as follows:
pnlDate.Visible = (selVal == "date"); PnlSubject.Visible = (selVal == "topic"); Pnlofficer.Visible = (selVal == "official"); PnlCIA.Visible = false; PnlMedia.Visible = false; PnlStatus.Visible = (selVal == "status"); This is all about visible = false;
and then only showing the item you need is showing because it only has real visibility settings in just 6 lines.
Comments
Post a Comment