Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch enum auto-fill

I was typing a switch with an enum in VS 2013 and all case statements filled out automatically after I finished the switch. Now I can't repeat it. I was not hallucinating, the switch filled out with all enum options, one per case, automatically. Looked through MS docs for VS 2013 and didn't find it.

I use quite a few enums and this feature will save me a ton of time if I can find what it is I did to trigger it. Can anyone help?

like image 925
Metaphor Avatar asked Nov 04 '13 22:11

Metaphor


3 Answers

Notice: This answer applies to performing the switch/enum autogeneration while also using Resharper.

Using Visual Studio 2013 and Resharper 8.2, the previously mentioned methods do not work. Here's how to actually get this generation to work when using Resharper. Hopefully it will save someone the fifteen minutes I just spent figuring this out.

Performing "sw(tab)(tab)" will only generate the following:

switch (nameOfEnumVariable)
{

}

Resharper can generate the labels using Alt + Enter (if your cursor stands in the switch statement) and selecting Generate switch labels as in the following screenshot:


Using the Resharper menu to fill in a switch statement


The result looks like this:


enter image description here


like image 150
jmsb Avatar answered Nov 05 '22 04:11

jmsb


Use the code snipped sw(tab)(tab)

Hope this helps,

like image 41
Marvin Smit Avatar answered Nov 05 '22 02:11

Marvin Smit


The selected answer is mostly correct, you don't need Resharper as other's have suggested (at least not with Visual Studio Professional 2012+).

1) type "sw" then "[tab][tab]" (as Marvin Smit said)

Which (as jmblack said) will generate something like:

  switch (switch_on)
  {
            default:
  }

but then

2) you need to select which thing to enumerate on (switch_on will be highlighted still at this point). So type in the Enum (or your variable of the enum type) while switch_on is highlited and hit [Enter][Enter].

(I just confirmed this worked on my machine running VS2012, and i'm fairly certain this is the same thing i have done on my other machine running VS2013, and i haven't tested other versions of VS (ultimate/express/etc.))

like image 31
00jt Avatar answered Nov 05 '22 04:11

00jt