Several people at DevTeach came up to me after sessions asking about an Intellisense Code Snippet I use to create properties in code, so I figure I'll post it here. Some of you might find it useful – I use the hell out of this particular one.
I'm always surprised that there are so many people who don't know about this powerful facility – this is one feature that you should familiarize with because it can save boatloads of keystrokes.
Code Snippets are a new feature in Visual Studio 2005 that allow you to create fairly flexible 'macros' that can inject code into the current editor document. The snippets are smart enough to let you substitute text and jump between the replaced text.
Snippets are activated by default by typing the shortcut keys (prop in this case) and then pressing Tab twice. They also show up as Intellisense 'keys' that you can discover which makes this whole process even more useful.
Visual Studio actually ships with a new property snippet in the box, but I don't really like the the way it formats the generated text.
I prefer my property definitions to look like this:
public object BindingSourceObject
{
get { return _BindingSourceObject; }
set { _BindingSourceObject = value; }
}
private object _BindingSourceObject = null;
The script allows replacing the type, name and the default value. I also much prefer to have the private field follow the public Property. The script also uses the most common default - using string values and empty string defaults. The nice thing about this script mechanism is that it is so easily customizable to exactly the way you like to work. I suspect others may want different defaults or a different layout, but it's trivial to change all that.
Some people will also like to automatically generate the comment snippet, but I actually don't believe in auto-generating comments – either document with something productive or leave the damn comment off <g>. That way at least the compiler can tell you got work to do rather than get a comment that's actually worse than no comment at all (isn't this how MS documents? <g> Some MSDN topics sure seem that way…)
Anyway here's the snippet (note this snippet is for C# obviously):
<?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>prop</Shortcut>
<Description>New Property (Rick Custom)</Description>
<Author>Westwind Technologies</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<ToolTip>Property Name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Object>
<ID>type</ID>
<ToolTip>Property Type</ToolTip>
<Default>string</Default>
</Object>
<Literal>
<ID>InitialValue</ID>
<ToolTip>Initial Value</ToolTip>
<Default>""</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public $type$ $name$
{
get { return _$name$; }
set { _$name$ = value; }
}
private $type$ _$name$ = $InitialValue$;
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
To use the snippet create a file called prop.snippet and copy it to:
My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets
The snippet above is a C# snippet. If you create a snippet for VB copy it to the Visual Basic folder.
Note PROP overrides an existing template in VS.NET so you'll see both the built-in snippet and the new one. If you rather have a unique name change the file name and the <shortcut> key in the XML. I use Prop because it seems like the natural way to use this particular shortcut.
I got similar snippets I use for classheaders and copy right notices, to do notices and inline change notes I leave to myself from time to time. I love to talk to my self after all…
I really like the extensibility in VS 2005 – all things user installed can be simply placed in these 'template' folders and they add to the default installation templates, snippets, add-ins, visualizers etc. that are provided by the default installation. This makes life a lot easier than in previous versions of VS.