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.

Comments

blog comments powered by Disqus

Download

Property Snippet

Next Post
Open for Comments  06 Apr 2008
Prior Post
Niche Software - Relaunched  31 Mar 2008
Related Posts
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
When are you done?  18 Apr 2022
Fixing GitHub Authentication  28 Nov 2021
Archives
April 2008
2008