Code refactoring c# question around several if statements performing the same check

Posted by James Radford on Stack Overflow See other posts from Stack Overflow or by James Radford
Published on 2011-06-27T11:58:34Z Indexed on 2011/06/27 16:22 UTC
Read the original article Hit count: 207

Filed under:
|
|

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++;
}
...

© Stack Overflow or respective owner

Related posts about .NET

Related posts about mvc