roductive developers and coding shops all maintain some type of custom code or scripts library, usually chock-full of code routines for common tasks such as string parsing, database operations, etc. Unfortunately, the mechanism used to store and retrieve such code clipsoften simply a vast directory tree full of text files saved on a local or external drive or stored on a network file serverhas also been custom-built.
In some cases, these home-built source code libraries are convenient, but just as often, because code and coding practices change rapidly, they end up becoming stale or out-of-sync with development trends. With Visual Studio 2005, Microsoft has finally stepped up to offer a generic, supported, and efficient way to manage and access those code librariesVisual Studio .NET snippets.
If the word "snippets" causes you to immediately make assumptions about the intent, you're probably correct. Snippets are little code blocks commonly used for repetitive tasks. Storing source code files locally in My Documents may be opportune, but it's not scalable, and is prey to human error. Visual Studio stores snippets in XML; they support replaceable parameters, assembly references and Imports
for C#) statements. Moreover, they provide an efficient way to organize and store reusable code with a level of scalability that was difficult to achieve with custom solutions.
Snippet files are simply well-formed and valid XML files with a .snippet
extension, placed where Visual Studio can find them. Although the Beta 2 release of VS.NET does not yet have a snippet editor (I stress yet
), it does come with a Snippets Manager to help you administer it all. For those who insist on a snippet editor immediately, you can download a free VB.NET Snippet Editor from MSDN
. The free Snippet Editor is a shared-source project; so I encourage you to download the source files, make enhancements or fixes as you see fit, and check in your modifications to help improve the product.
I'll start out by showing you the basics of the Snippets Manager using stock snippets included with VS.NET 2005 Beta 2. Then you'll see how to create new XML snippets, how to add them to the Snippets Manager, and finally, how to package them for distribution.
Visual Studio Snippets Manager
To get started, open VS.NET 2005 Beta 2 and begin a new project. When the New Project dialog window opens, expand the Visual Basic item in the Project Types pane. Select Windows, and in the Templates pane, select Windows Application. If you prefer, change the name of the application and change the location where the application is to be saved. Click OK (see Figure 1
|Figure 1. Create a New VB.NET Application: The figure shows the New Project dialog, in the process of creating a new Windows application project.|
After the project loads, it defaults to design-time mode and displays a blank form. Switch to code view using the Solution Explorer tool bar, and you'll see the default template that begins a public class named form1
. At this point, I usually create a few named Regions. Regions, in Visual Studio, set collapsible blocks of code, giving you the ability to organize and hide portions of your code, just like Classes, Functions or Methods. I name these Regions "methods," "functions," and "declarations."
For this example, you're going to create a method to handle a basic file system task and a function that generates and returns a random number.
First, create a method named copyFiles
. Next, press Enter to close the method, and place your cursor between the opening and closing statements. This is where you'll add the snippet.
' place your cursor here
The snippet code for this example already exists in the snippet libraries that ship with Visual Studio. Open the Snippets Manager by pressing Ctrl+K+B
and make sure Visual Basic is selected in the Language ListBox. In the left pane you'll see a directory tree of all the snippet's categories. Expand the filesystem
folder and select the "Copy a File" snippet. When you select a snippet, the Snippets Manager displays the path to the selected snippet file in the location bar, and displays a description of the snippet, the shortcut phrase, and the author in the right pane (see Figure 2
). Alternatively, you can place your cursor where you want the snippet inserted and press Ctrl+K+X
, to open Snippet Intellisense, which then displays available snippets based on the language of the current project.
|Figure 2. Selecting a Snippet: Open the Snippets Manager and select the Copy a File snippet in the filesystem folder.|
To insert the "Copy a File" snippet code, make sure the cursor is inside your copyFiles
method, and then type the shortcut phrase shown in the right pane (filCopyFile
) and press the Tab key. Visual Studio inserts the snippet code into your method, replacing the shortcut phrase, and highlights any replaceable parameters.
Next, create a method named getRandNum()
that will generate and return a random integer.
Private Function getRandNum() As Integer
to open the Snippets Manager and expand the math folder. Select the "Get a Random Number using the Random class" snippet. Type the shortcut phrase into your function and press Tab. As before, this will insert the snippet into your function and highlight the replaceable parameters. To complete your function, add a Return
statement after the snippet code and pass the variable named randomValue
|Figure 3. Form Code with Inserted Snippets: The figure shows the code view of the sample form after completing a method and a function by inserting Visual Studio snippets.|
Private Function getRandNum() As Integer
Dim generator As New Random
Dim randomValue As Integer
' Generates numbers between 1 and 5, inclusive.
randomValue = generator.Next(1, 6)
In both the examples shown here, you can substitute your own variables or constants for the replaceable parameters, and place a copy of the variables in the functions parameter list. Just as with any code in Visual Studio, you will have real-time debugging and Intellisense. Your class should look like Figure 3