Using the switch code snippet with an enum

by Henrik Stenbæk 27. March 2008 18:32

I just had one of those WOW experience today when I happened to use the switch code snippet with an enum.

I had an enum like this:

   1:  enum MyEnum
   2:  {
   3:      value_1,
   4:      value_2,
   5:      value_3,
   6:      value_4,
   7:      value_5
   8:  }

Start the video to se what happened when I tapped out of the snippet "switch_on" field.

WOW the snippet automatically created:

case MyEnum.value_1:
    break;

for each element in the enum. But how did it do that? Looking into the switch.snippet file I found:

<Literal Editable="false">
    <ID>cases</ID>
    <Function>GenerateSwitchCases($expression$)</Function>
    <Default>default:</Default>
</Literal>

 

It's the GenerateSwitchCases function that's doing the job - one out of 3 pre defined functions that available to "snippets"

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories: snippets
Comments are closed