harriyott.com

Wednesday, December 05, 2007

Visual Studio 2008 Snippets

In Visual Studio 2005, there was a handy snippet to generate a property, which included a private variable and a public get and set, appropriately coded. By typing prop and pressing tab, you got the following, which helped you choose the names:

Snippet for Visual Studio 2005 properties

With VS2008, there is a handy new feature, automatic properties, that work in the same way, but don't need the private member, the return or the assignment. This too has a snippet:

Snippet for Visual Studio 2008 properties

Cool. What's also cool is that you can compile .NET 2.0 projects in VS2008 as well as 3.0 and 3.5 without switching IDEs. So, whilst working on my .NET 2.0 project, I typed prop and tabbed as usual, and got the new snippet. Which didn't compile. A nice touch would have been to have different snippets for different framework versions, automatically selected by the version the project was set to. I'm not sure whether to call this a bug or not, but it is certainly slightly irksome.

4 Comments:

Blogger The Moth said...

What didn't compile?

December 06, 2007 10:40 AM  
Anonymous Simon said...

The property generated. To reproduce:

1. Open a VS2005 project in VS2008.
2. Run the "prop" snippet in a class as usual, leaving the defaults as int and MyProperty.
3. Compile, and note the two errors: "_Default.MyProperty.get' must declare a body because it is not marked abstract or extern", and the same for the set.

December 07, 2007 10:00 AM  
Anonymous Daniel Moth said...

sorry, came back to this thread by accident and discovered it was still open

Under VS2008 it should have compiled fine regardless of framework and it will work at runtime.

I think you are compiling it under VS2005 *after* editing in VS2008, which of course won't compile (and of course, you haven't stated anywhere that you are re-opening it under VS2005 ;)).

February 22, 2008 6:58 PM  
Anonymous Simon said...

Not so, good fellow.

I'm not opening the project in VS2005, and not changing runtime versions.

The same snippet is used whichever version the project is compiled as. In this case, the snippet was installed with VS2008 to c:\Program Files\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\prop.snippet. Examining the file, the actual code is:

public $type$ $property$ { get; set; }$end$

As this snippet is run irrespective of framework version, it generates code that won't compile in .NET 2.0 projects.

The correct implementation of snippets in VS2008 should be to have different folders for different versions, and different snippets of the same name can be run accordingly.

February 22, 2008 7:09 PM  

Post a Comment

Links to this post:

Create a Link

<< Home