Switch Statement Code Snippet

Another Visual Studio tip – a colleague of mine showed me the “switch” code snippet. It automatically builds a complete switch statement with a case for each value in an enumeration. This can really save some time.

Here’s an example:

public class Car
{
    public enum ColorName { Red, Green, Blue, Yellow, Purple, Pink, Gray, Silver, Black };
    public ColorName BodyColor { get; set; }
 
    public Car()
    {
       
    }
}

Inside the constructor, type the following:
1) Type “switch”
2) Hit the TAB key twice
3) Now over-type the “switch_on” text with your property name (of type enum).
4) Click outside the switch statement

You should have this:

public class Car
{
    public enum ColorName { Red, Green, Blue, Yellow, Purple, Pink, Gray, Silver, Black };
    public ColorName BodyColor { get; set; }
 
    public Car()
    {
        switch (BodyColor)
        {
            case ColorName.Red:
                break;
            case ColorName.Green:
                break;
            case ColorName.Blue:
                break;
            case ColorName.Yellow:
                break;
            case ColorName.Purple:
                break;
            case ColorName.Pink:
                break;
            case ColorName.Gray:
                break;
            case ColorName.Silver:
                break;
            case ColorName.Black:
                break;
            default:
                break;
        }
    }
}
Advertisements
This entry was posted in Tips and Tricks and tagged , , . Bookmark the permalink.

One Response to Switch Statement Code Snippet

  1. Bernhard says:

    thanks – that kind of saved me numerous minutes of typing today šŸ˜€

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s