Code refactoring c# question around several if statements performing the same check
- by James Radford
I have a method that has a load of if statements that seems a bit silly although I'm not sure how to improve the code.
Here's an example. This logic was inside the view which is now in the controller which is far better but is there something I'm missing, maybe a design pattern that stops me having to check against panelCount < NumberOfPanelsToShow and handling the panelCount every condition? Maybe not, just feels ugly!
Many thanks
if (model.Train && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Train);
panelCount++;
}
if (model.Car && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Car);
panelCount++;
}
if (model.Hotel && panelCount < NumberOfPanelsToShow)
{
panelTypeList.Add(TheType.Hotel);
panelCount++;
}
...