One of the less well known features in Visual Studio are the snippets - fragments of code ready for insertion into your code at a moments notice. In Visual Studio 2005, the prop snippet was a very useful way to declare new class properties, but in Visual Studio 2008 they crippled it. Since you can create your own snippets, I created a better version of propthat works the way I want.

In Visual Studio 2005, the prop snippet expanded into a full property declaration, including a member variable for underlying storage.

Prop Snippet from Visual Studio 2005

I found the prop snippet extremely useful as a base to work from. For example, when writing complex classes that implement INotifyPropertyChanged, I’d use this snippet as a base and then add the required code to trigger the PropertyChanged event when required.

In Visual Studio 2008, however, they evidently assumed that people never customised the output of the prop snippet, as the expansion has changed considerably:

Prop Snippet from Visual Studio 2005

While automatic property implementation is fairly cool, I’ve found this version of the prop snippet to be much less useful.

Fortunately, you’re not restricted to the snippets that come out of the box - you can create your own. For full details on how to do this, check out the MSDN page Creating Code Snippets

In my case, since I’m so often wanting to include support for the PropertyChanged event, I decided to create a snippet that already includes the code required. I’ve called it propn - short for property with notification.

Here’s how the snippet expands:

Prop Snippet from Visual Studio 2005

Since I use a convention that the name of the member variable matches the name of the property, prefixed with m for member, this snippet has only two fields - property type and property name. You can see that support for the PropertyChanged event is built in, including the necessary logic to only trigger the event if the value assigned is different. Finally, the whole property is wrapped in a region of its own - this is so I can collapse the property down in the Visual Studio editor when I’m not interested in the implementation.

If you’re interested in using the propn snippet, download the zip file.


blog comments powered by Disqus