Code Snippets

Published 29 September 2005 16:42 by Derek Lakin
Filed Under: , ,

Code Snippets are great! Just type '?' and then hit <TAB> and you can choose from the available snippets or you can right-click in the code editor and choose "Insert Snippet..." or you can type the abbreviation for a snippet and hit tab.

Once you've inserted your snippet you can then just hit <TAB> to cycle through the fields you need to fill in. So far I've been using "prop" to insert a new private field with a matching public property (you can use "propg" to insert a read-only property).

My personal preference is to prefix member variables with "this." (partly because it provides a visual cue as to whether the variable is local or a member, but mostly because it's a quick way to get intellisense!) and to my delight I found that the snippets are just XML files with a .snippet file extension stored in \Microsoft Visual Studio 8\VC#\Snippets\1033\.

It's a really simple modification to just change the template to add "this." in front of the right parts but in case you're too lazy too do it yourself I've included my modified version in my Code Snippets shared files here on imeem.

There is a Snippet Editor available and it's an Open Source effort hosted on GotDotNet:

"Are you eager to produce your own Visual Basic Code Snippets but prefer not to think in terms of angle brackets? The Visual Basic Snippet Editor is a Windows Forms application with UI for creating, editing, testing VB code snippets. You can specify snippet code and metadata (e.g., title, author, description, keyboard shortcut), replacement variables, and referenced assemblies. You can preview your snippet as it will appear when inserted into the code window, and you can even test whether it will compile."

Unfortunately, it seems to only work on the Beta 2 release of VS, but source code is provided so presumably I could re-compile and try again.

Comments