Kevin McNeish Blog

All things iOS, Kindle and .NET

Recent Posts

Tags

News

  • First books in my new book series, "iOS App Development for Non-Programmers" are now available! iBookStore: http://itunes.apple.com/us/book/book-1-diving-in-ios-app-development/id558788074?mt=11 Amazon: http://www.amazon.com/dp/B0097N8XBE Amazon: http://www.amazon.com/dp/B0099RQGMQ

Community

Email Notifications

Archives

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

Comments

fab@fab-silva.com said:

very coll snippet.  Got any more?

# June 14, 2007 1:49 PM
Leave a Comment

(required) 

(required) 

(optional)
 

(required) 

If you can't read this number refresh your screen
Enter the numbers above: