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

Fixing "Rebuild" error when using the Object Test Bench

Lots of folks don’t know that Visual Studio 2005 has design-time expression evaluation that allows you to instantiate objects and execute their methods at design time–a very cool feature. You can do this by right-clicking an object in a class diagram and selecting Create Instance from the shortcut menu (you can also do the same from the Class View window).


If you get an error telling you to rebuild the project when trying to instantiate an object in the Object Test Bench (and the error won’t go away), rebuild your project, then select Tools | Options from the Visual Studio menu and in the Options dialog, select Projects and Solutions in the left pane then mark the Show advanced build configurations check box.


Best Regards,

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