5

This question is kind of an add-on to this question

In C#, a switch case cannot fall through to other cases, this causes a compilation error. In this case I am just adding some number to the month total for the selected month and each subsequent month thereafter. (simple example, not meant to be real)

switch (month)
{
    case 0:
      add something to month totals
    case 1:
      add something to month totals
    case 2:
      add something to month totals
    default:
      break;
}

Is there a logical alternative to this in C# without having to write out a ton of if statements?

if (month <= 0)
   add something to month
if (month <= 1)
   add something to month
if (month <= 2)
   add something to month
.... etc
Community
  • 1
  • 1
Mike Fielden
  • 10,055
  • 14
  • 59
  • 99

5 Answers5

11

Often times when you see the noise from a huge switch statement or many if statements that might fall into more than one block, you're trying to suppress a bad design.

Instead, what if you implemented the Specification pattern to see if something matched, and then act on it?

foreach(MonthSpecification spec in this.MonthSpecifications)
{
   if(spec.IsSatisfiedBy(month))
       spec.Perform(month);
}

then you can just add up different specs that match what you're trying to do.

It's hard to tell what your domain is, so my example might be a little contrived.

Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
8

In C# switch statements you can fall through cases only if there is no statement for the case you want to fall through

switch(myVar)
{
   case 1:
   case 2: // Case 1 or 2 get here
      break;
}

However if you want to fall through with a statement you must use the dreaded GOTO

switch(myVar)
    {
       case 1: // Case 1 statement
               goto case 2;
       case 2: // Case 1 or 2 get here
          break;
    }
jwarzech
  • 6,596
  • 11
  • 52
  • 72
  • 2
    Why is GOTO dreaded? Also, use of "goto case" is not a "goto statement". – Ben Voigt Apr 01 '10 at 23:59
  • 4
    I think using the word 'dreaded' is justified. I could easily imagine programmers miss-using the `goto case` in a switch statement to make it jump from case to case and back to the original case. Goto leads to spaghetti, spaghetti leads to hate, hate leads to suffering. Goto is the path to the dark side. – Ben Jan 14 '15 at 16:32
1

There is already a question addressing this topic:

C# switch statement limitations - why?

EDIT:

My main purpose in pointing that out, gentlebeasts, is that two questions of near-identical name add confusion to the pool of questions.

Community
  • 1
  • 1
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
  • 1
    I don't see the relation (apart from the title). Question 44905 is about the origins of certain limitations. This question is about how to overcome one specific limitation (that isn't even mentioned in the other question). – mweerden Sep 09 '08 at 16:51
  • 2
    If you want to point out that there is a problem with the title, then perhaps you shouldn't add a tag `duplicate` but either change the title or explicitly mention that you think there is a problem it. – mweerden Sep 09 '08 at 17:05
1

Are you adding constants? If so, maybe something like this would work(C syntax):

const int addToTotals[] = {123, 456, ..., 789};

for(i=month;i<12;i++)
   totals += addToTotals[i];

You can do a similar thing with variable or function pointers if you need more complex statements than add constant to totals for each month following.

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
0

Write the switch cases in reverse order

case 2:

case 1:

case 0:

break;


default:

Hope that helps!

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
svv
  • 1
  • 1
    C# still requires a flow-control statement (break/continue/return/goto/goto case/throw) between cases if any other code is present. – Ben Voigt Apr 02 '10 at 00:00