patterncsharpMinor
Cleaning up a redundant switch statement
Viewed 0 times
redundantswitchcleaningstatement
Problem
How can I use a more generic method to clean up the redundancy in the switch statement below?
I'm working on an ASP.NET webforms app that will have five identical user controls that I will be showing and hiding based on the value of a dropdown list. (I have already been down the path of trying to add and remove the controls dynamically--the issue of managing the view states for the controls would have been very complicated, so in the interest of shipping I'm opting for a fixed number of controls.)
The approach I'm currently using works but isn't very elegant:
What are some options here? The best I can come up with is using numberOfControls to build the name of the control, but that seems hacky. Suggestions appreciated.
EDIT: I implemented a solution similar to the accepted answer below. I'm stuck on loading up the controls in the list. The commented out code is more along the lines of what I'd like to do but can't get it working. The uncommented code works. Suggestions?
```
private List PopulateShiftControlList()
{
ShiftControl shiftControlList = new List();
//ControlCollection panelControls = ShiftPanel.Controls;
//foreach (ShiftControl control in panelControls)
//{
// shiftControlList.Add(control);
//}
shiftControlList.Add(ShiftControl1);
shiftControlList.Add(ShiftControl2);
shiftControlList.Add(ShiftControl3);
shiftControlList.Add
I'm working on an ASP.NET webforms app that will have five identical user controls that I will be showing and hiding based on the value of a dropdown list. (I have already been down the path of trying to add and remove the controls dynamically--the issue of managing the view states for the controls would have been very complicated, so in the interest of shipping I'm opting for a fixed number of controls.)
The approach I'm currently using works but isn't very elegant:
protected void DisplayUserControls(int numberOfControls)
{
switch (numberOfControls)
{
case 2:
UserControl1.Visible = true;
UserControl2.Visible = true;
UserControl3.Visible = false;
break;
case 3:
UserControl1.Visible = true;
UserControl2.Visible = true;
UserControl3.Visible = true;
break;
default:
UserControl1.Visible = true;
UserControl2.Visible = false;
UserControl3.Visible = false;
break;
}
}What are some options here? The best I can come up with is using numberOfControls to build the name of the control, but that seems hacky. Suggestions appreciated.
EDIT: I implemented a solution similar to the accepted answer below. I'm stuck on loading up the controls in the list. The commented out code is more along the lines of what I'd like to do but can't get it working. The uncommented code works. Suggestions?
```
private List PopulateShiftControlList()
{
ShiftControl shiftControlList = new List();
//ControlCollection panelControls = ShiftPanel.Controls;
//foreach (ShiftControl control in panelControls)
//{
// shiftControlList.Add(control);
//}
shiftControlList.Add(ShiftControl1);
shiftControlList.Add(ShiftControl2);
shiftControlList.Add(ShiftControl3);
shiftControlList.Add
Solution
I would create a list of usercontrols. Based on the amount of controls that need to be visible, traverse the list and set x controls to visible.
Or rather in one go:
protected void DisplayUserControls(List controls, int numberOfControls)
{
Contract.Requires( numberOfControls >= 0 && numberOfControls <= controls.Count );
for ( int i = 0; i < numberOfControls; ++i )
{
controls[ i ].Visible = true;
}
for ( int i = numberofControls; i < controls.Count; ++i )
{
controls[ i ].Visible = false;
}
}Or rather in one go:
protected void DisplayUserControls(List controls, int numberOfControls)
{
Contract.Requires( numberOfControls >= 0 && numberOfControls <= controls.Count );
for ( int i = 0; i < controls.Count; ++i )
{
controls[ i ].Visible = i < numberOfControls;
}
}Code Snippets
protected void DisplayUserControls(List<UserControl> controls, int numberOfControls)
{
Contract.Requires( numberOfControls >= 0 && numberOfControls <= controls.Count );
for ( int i = 0; i < numberOfControls; ++i )
{
controls[ i ].Visible = true;
}
for ( int i = numberofControls; i < controls.Count; ++i )
{
controls[ i ].Visible = false;
}
}protected void DisplayUserControls(List<UserControl> controls, int numberOfControls)
{
Contract.Requires( numberOfControls >= 0 && numberOfControls <= controls.Count );
for ( int i = 0; i < controls.Count; ++i )
{
controls[ i ].Visible = i < numberOfControls;
}
}Context
StackExchange Code Review Q#1091, answer score: 9
Revisions (0)
No revisions yet.