Essential Visual Studio Tips & Tricks

While doing my morning reading, I happened across this post on Stephen Walther’s blog via one of Scott Guthrie’s "Links" blog post. If you use Visual Studio, you need to read that post!

While I found most of the tips useful to my every day programming, none other caught my attention as much as Tip #3: Never Create Properties By Hand.

Never type property declarations by hand. It takes forever! Instead, just type prop + TAB + TAB. When you type prop + TAB + TAB, you get a code snippet (template) for entering a property. Use TAB to move between the template parameters. Press the ENTER key when you are finished creating the property (see Figure 4). This tip has saved me from many days of tedious property typing.

This tip will save me tons of time, and it makes me wish I investigated the snippet feature the first day I started using VS. I always knew VS had snippets, but I didn’t know it was as robust as it is.

Anyway, we use VS 2005 here at work, and when I tried the prop + TAB + TAB I was delighted to see VS provide me with both private and public (with getters and setters) members. However, when I fired up Visual Web Dev 2008 and tried the same key strokes, VWD only gave me a public property with get and set stubs. I find the 2005 version more suited for my needs. Here’s the XML for the 2005 version if you want to add it to 2008. I renamed the command to propp (the trailing p is for private). You can copy the XML, save it as a .snippet file, and import it using the Snippet Manager in the Tools menu.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propp</Title>
            <Shortcut>propp</Shortcut>
            <Description>Code snippet for property and backing field</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>The variable backing this property</ToolTip>
                    <Default>myVar</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[private $type$ $field$;

    public $type$ $property$
    {
        get { return $field$;}
        set { $field$ = value;}
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
10/23/2008 10:23:19 AM | Tags: Visual Studio
© 2008 Jeremy McPeak