How can I combine case labels in C# 1.1?

Based on the font and its size identifer, I want to calculate the height of the text. This, though, doesn't compile (complaining "Control cannot fall through from one case label ('default:') to another"):

    private int GetHeightForFontAndFontSize(int fontType, int fontSizeId)
    {
        int retVal = 0;
        if (fontType == 0)
        {
            switch (fontSizeId)
            {
                case 0:
                case 1:
                    retVal = 9;
                    break;
                case 2:
                case 3:
                case 4:
                    retVal = 18;
                    break;
                case 5:
                case 6:
                    retVal = 36;
                    break;
                default:
                    retVal = 9;
            }           
        }
        if (fontType == 1)
        . . .

                          return retVal;
    }

...even though this: http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx says that "Although fall through from one case label to another is not supported, it is allowed to stack case labels, for example"

It seems to me I AM stacking the case labels as they depict, so what is the problem?

Upvotes: 0

Views: 306

Answers (3)

Hooch
Hooch

Reputation: 29673

Here is fixed code. I added break in default case.

private int GetHeightForFontAndFontSize(int fontType, int fontSizeId)
    {
        int retVal = 0;
        if (fontType == 0)
        {
            switch (fontSizeId)
            {
                case 0:
                case 1:
                    retVal = 9;
                    break;
                case 2:
                case 3:
                case 4:
                    retVal = 18;
                    break;
                case 5:
                case 6:
                    retVal = 36;
                    break;
                default:
                    retVal = 9;
                    break;
            }           
        }
        if (fontType == 1)
        . . .

                          return retVal;
    }

Upvotes: 1

Twiltie
Twiltie

Reputation: 572

Try putting a break; after default.

Upvotes: 1

SLaks
SLaks

Reputation: 887245

You need a break; for the default: case.

Upvotes: 5

Related Questions