Eiver
Eiver

Reputation: 2635

How to refresh switch statement cases automatically in Visual Studio?

I have previously generated a switch statement over an enum using the automatic snippet included in VS 2010 (I pressed sw [tab] [tab]). Later I extended the enum type. It now contains more values. Now I want to update the switch statement, so that it again includes cases for all possible enum values. Is it possible to automatically generate all missing cases for the enum while keeping existing ones, rather than doing that manually?

Upvotes: 1

Views: 1847

Answers (4)

Piotrek
Piotrek

Reputation: 189

I know this is an extremely old one, but if some is still interested in the VS 2019 you can just click on your 'switch', then 'Quick Actions and Refactoring' and in the menu there is 'Add missing cases" option.

Upvotes: 2

Thomas Waldner
Thomas Waldner

Reputation: 1

What you need to do is sw[tab][tab] - which should give you something like "switch (switch_on)" where "switch_on" is marked. Now start typing your enum name - it is also possible to have SomeOtherClass.SomeOtherClassesEnum here - just don't start moving the cursor around. When you have the full name inside the parenthesis, hit [Enter] - boom, all enum-cases + default is there.

Upvotes: 0

Som Shekhar
Som Shekhar

Reputation: 138

Just found an interesting trick to solve this. Was frustrated to revisit this issue again and again.

I created a new switch statement just below the previous one where I typed sw(tab)(tab) to create the autogenerated syntax. Then I entered the enum variable into the switch code to generate the complete list of all possible options.

I copied all the options leaving the default and pasted just above the default option in the previous switch section.

Visual Studio is smart enough to show the duplicate options and all I had to do was to delete those.

Still a much faster option than looking through the complete list.

Upvotes: 1

demoncodemonkey
demoncodemonkey

Reputation: 11957

As far as I can see there is no way to do that. Not even with Resharper. Your best bet would be to use find in files to locate all instances of a switch statement containing that enum.

Example: if your enum was called Bob, then the generated switch statement probably looks like this:

        switch (someValue)
        {
            case Bob.one:
                break;
            case Bob.two:
                break;
            case Bob.three:
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }

So in this case you could do a Find In Files for "case Bob." and then manually add a case for the new item.


Edit:
@RichieHindle's suggestion is quite cool. Basically do a regex Replace In Files to append your new case to an existing one. In the example above, say you wanted to add a new item to Bob called zero, you'd do a regex Replace In Files for case Bob\.one: with:

case Bob\.zero:\nthrow new NotImplementedException\(\);\ncase Bob\.one:

(might have to mess around with the regex, but that should give you a good start)

Upvotes: 3

Related Questions