Property Code Snippet for Visual Studio 2005

In contrast to the default Visual Studio format, I prefer to format my properties like this with the associated variable beneath the property declaration and a comment above it <s>:

/// <summary>
/// OrderID Property
/// </summary>
public int OrderID
{
    get { return _orderID; }
    set { _orderID = value; }
}
private int _orderID;


My partner in crime Rick Strahl (www.west-wind.com) wrote a blog post about a snippet he created do something similar which gave me the idea to do it to suit my own needs. Here’s what I’ve come up with:

<?xml version=1.0 encoding=utf-8 ?>
<CodeSnippets xmlns=http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet>
    <
CodeSnippet Format=1.0.0>
       
<Header>
            <
Title>prop</Title>
            <
Shortcut>prp</Shortcut>
            <
Description>Code snippet for property and backing field</Description>
            <
Author>Kevin McNeish</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>_var</Default>
                </
Literal>
            </
Declarations>
            <
Code Language=csharp>
                <![CDATA[
/// <summary>
   
/// $property$ Property
    /// </summary>
    public $type$ $property$
    {
        get { return $field$;}
        set { $field$ = value;}
    }
    private $type$ $field$;
$end$
]]>
            </
Code>
        </
Snippet>
    </
CodeSnippet>
</
CodeSnippets>


To use this snippet, create a file named prp.snippet and place it in this folder on your computer:


My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets


The keyword to fire this snippet is “prp” (rather than “prop”). Obviously this snippet can also be adapted for VB .NET.


Note that in order to get camel case to work properly for the private variable (since you can’t call a function from a snippet) after typing _myVariable in the Get() accessor you need to press ENTER rather than TAB!


Best Regards,


Kevin McNeish
Microsoft .NET MVP
President, Oak Leaf Enterprises, Inc.
Chief Architect of the MM .NET Application Framework

One thought on “Property Code Snippet for Visual Studio 2005”

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>