Building Your Own Snippet
To get the greatest benefit from snippets, you will want to create your own. There are several ways you can approach this task.
You can build your own snippets the hard way by manually creating the snippet file contents yourself with your XML editor of choice. But this means that you have to have an intimate understanding of the schema of the XML and have to do a lot of typing, typing, typing.
|If you use the XML code window provided in Visual Studio 2005, you will get some assistance with creating your snippet.|
If you use the XML code window provided in Visual Studio 2005, you will get some assistance with creating your snippet. The XML code window Code Snippet Picker provides a Snippet snippet. This snippet provides you with the correct XML layout for a snippet XML file. All you need to do is tab through it to set the appropriate element values and then add the code you want for the snippet itself.
Another choice is to locate a snippet file that is similar to the snippet that you want to create. Copy the file, then edit it as you desire. This is the best choice if you simply want an enhanced version of one of the existing snippets.
By far the easiest way to create your own snippets is to use the open source Snippet Editor
created by Bill McCarthy
(Check out his blog
), a developer from Australia and Microsoft MVP, in conjunction with Microsoft.
After downloading the Snippet Editor
, launch the file SnippetEditor.exe
. By default, the Snippet Editor displays Visual Basic snippets in a folder hierarchy in the left pane. Note that this is the same folder hierarchy as displayed in the Code Snippets Manager except that the snippet name is displayed instead of the title. To add snippets for other languages, use the Option button (on the far right of the toolbar) and select the desired language(s).
The Snippet Editor dialog allows you to edit existing snippets or create new ones. It allows you to add snippet folders and drag and drop snippets between the folders. It also allows you to build a Visual Studio Content Installer, making it easier to share your snippets with others. Most importantly, it allows you to search snippets by keyword using the filter feature just above the folder hierarchy.
For example, say you want to find any snippet having to do with properties. Type "prop" in the filter text box, click on the Apply button, and the folder hierarchy in the left pane of the Snippet Editor is filtered to only those snippets with the letters "prop."
Say you want to update the VB property snippet so that the private member variable is always prefixed with an underscore. Navigate the filtered folder hierarchy to the DefineAProperty
snippet. Double-click on the snippet to edit it. It will then appear in the Snippet Editor as shown in Figure 5
|Figure 5: Create your own Visual Basic snippets using the VB Snippet Editor. Note that this is an open source project and the user interface may change.|
The Editor tab in the top right pane of the Snippet Editor allows you to edit the snippet as desired. The Preview tab provides a preview of what the code expansion will look like so you can confirm your expected results.
The bottom right pane of the Snippet Editor provides tabs to assist you with the editing of your snippet. The Properties tab displays the properties of the snippet such as the title, description, and shortcut.
The Replacement tab allows you to define the fields that are to be used as replacements when the snippet is expanded. You can add new replacements, modify existing replacements, or delete a replacement.
The References tab defines which references need to be added when the snippet is inserted into the code window. Similarly, the Import tab defines which imports need to be set.
Create your own snippet by right-clicking on the desired folder and selecting Add New Snippet from the context menu.
For example, say you want to create a specialized property procedure that knows how to handle object instances. Create your own snippet for this under common code patterns, choose properties and procedures, and name it InstanceProperty
Double-click on the new InstanceProperty
snippet to edit it. On the Properties tab, give the snippet a title, description, shortcut, and so on.
On the Replacements tab, add a replacement for the data type, private variable name, and property name. Then write the desired code in the Editor pane (or copy from one of the other snippets and edit it), using the replacements as needed.
Private $PrivateVariable$ As $PropertyType$
Public Property $PropertyName$() As $PropertyType$
If $PrivateVariable$ Is Nothing Then
$PrivateVariable$ = New $PropertyType$
Set(ByVal value As $PropertyType$)
$PrivateVariable$ = value
Click on the Preview tab to confirm the results. When you obtain the desired results, save the snippet. The snippet is automatically added to the Code Snippet Manager in the defined folder. So it is immediately ready for your use in the code window.
Any time you find yourself typing the same set of code or looking up some syntax that you don't use very often, consider building a snippet for that code. As you build up your snippet library, you will have the code for many of your coding tasks right at your fingertips.
Try Out Snippets
Using snippets can provide a huge time savings, both in minimizing typing and in looking up syntax for commonly needed tasks.
Your snippets are yours. Modify them, reorganize them, and create your own so you can build a very efficient, personalized snippets library. And have fun!