How can I easily create a case (switch case) for all enum items?
Asked Answered
B

2

7

Often I am using a switch-case, and creating a case for all enum items.

Is there a shortcut (it may well be a resharper shortcut) which creates all cases automatically and just let me fill the cases?

Or is this question related to code-snippet subject? Is it possible to create a dynamic code-snippet (it will vary according to enum type) ?

Bittencourt answered 9/6, 2014 at 7:50 Comment(2)
There's a danger there that a: it doesn't adapt to code changes (new enum values, etc), and b: it doesn't handle to undefined values ([Flags], or just arbitrary values). Frankly, IMO - whatever IDE tooling you use, great, but adding a default case that throws a suitable exception should be fairly standard practice. And tests. It is notable that F# has better treatment here (at least, for discriminated unions)Frei
@MarcGravell You are right. In resharper option in default case it is throwing ArgumentOutOfRangeException.Bittencourt
C
12

Set the cursor after the first brace within the switch statement and press Alt + Enter. You will see the option to generate switch labels.

Cranford answered 9/6, 2014 at 7:57 Comment(7)
Is that resharper? I don't think that is VS vanilla.Frei
Yes it is a resharper shortcut.Cranford
Great, thank you. Yes it seems it is resharper short-cut. alt-enter gives resharper options.Bittencourt
Using my ReSharper 2016.2 it 'generate switch labels' isn't show if I press Alt+EnterMoskow
Using ReSharper 2016.2.2, generate switch labels does not work. A workaround for this is to enter Switch TAB+TAB (with a capital S), with tab+tab, it replaces Switch with switch, and then it auto-fills.Echoism
Same thing works in IntelliJ IDEa. Yaaay!Metaphysic
Answer by @Prerak K and Jesse Petronio both work, However, Jesse's solution gives you the option to include or exclude the default case, whilst Perak K includes the default case implicitly.Theorize
M
20

Plain VS snippet will work for you ..write switch and press "TAB", write your enum type and press enter(twice), the cases will be generated for you.

Muffin answered 9/6, 2014 at 8:1 Comment(0)
C
12

Set the cursor after the first brace within the switch statement and press Alt + Enter. You will see the option to generate switch labels.

Cranford answered 9/6, 2014 at 7:57 Comment(7)
Is that resharper? I don't think that is VS vanilla.Frei
Yes it is a resharper shortcut.Cranford
Great, thank you. Yes it seems it is resharper short-cut. alt-enter gives resharper options.Bittencourt
Using my ReSharper 2016.2 it 'generate switch labels' isn't show if I press Alt+EnterMoskow
Using ReSharper 2016.2.2, generate switch labels does not work. A workaround for this is to enter Switch TAB+TAB (with a capital S), with tab+tab, it replaces Switch with switch, and then it auto-fills.Echoism
Same thing works in IntelliJ IDEa. Yaaay!Metaphysic
Answer by @Prerak K and Jesse Petronio both work, However, Jesse's solution gives you the option to include or exclude the default case, whilst Perak K includes the default case implicitly.Theorize

© 2022 - 2024 — McMap. All rights reserved.