Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Corey Roth

Related Topics: .NET

.NET: Article

A First Look at Visual Studio 2005 Code Snippets

Why didn't we get this sooner?

Notice that the Declarations element contains the Literal and Object elements. These are what make automatic code replacement possible. The Literal element identifies a replacement placeholder for code that's contained entirely within the snippet. Examples of literals might be strings, integers, or variable names. The Object element identifies a placeholder, required by the snippet, which is likely to be outside the snippet itself. ASP.NET controls, Windows Forms controls, or an instance of an object or type make good candidates for this kind of element declaration.

There's also an optional element called Function that's only supported in Visual C# and Visual J# the details of which are beyond the scope of this article. However, I'll mention that there are only four built-in functions (you can't create your own) that essentially specify the function that executes when the literal or object receives focus in your code.

And finally, we come to the code. The code element defines the actual code and replacement items that will be inserted. Notice the Select...Case statement in Listing 2. The code element contains the text that's inserted within the CDATA brackets. The $ acts as a delimiter for the replacement literal and object. The attributes for the Code element are:

  • Kind - Specifies the type of code the snippet has and where the snippet can be inserted. (See http://msdn2.microsoft.com/en-us/library/ms171421.aspx for more information).
  • Delimiter - The value that delimits replacement variables. The default is: $.
  • Language - Values must be: VB, CSharp, VJSharp, or XML
Building a Snippet From Scratch
Let's look at the C# snippet in Listing 3 that I created using Notepad. Since C# doesn't have an IsNumeric function, unlike VB, I created one using this snippet. It will be the beginning of a snippet library that I'll keep for future use. The file is named IsNumeric.snippet.

The two Literal values in Listing 3 demonstrate the use of replacements after the code has been inserted. The replacements in the comments section are highlighted after the insert is executed. The developer then has to change the class name and the developer's values. (Figure 4)

Making Your Snippet Available for Use
Now that we've created an XML snippet file, we need to make it available to the snippet picker using the Code Snippet Manager tool in VS 2005. In the VS IDE, select Tools | Code Snippet Manager. Here you can browse the snippet folders and gain information about the snippets you've installed.

When you select a snippet from your list, you'll see its description, shortcut, snippettype, and author information. You'll only see snippets here because the manager filters to only show snippet files.

The Add button should actually be renamed Add Folder, because that's what it really does. It lets you select and add a folder containing snippets, not individual snippets themselves. It should be noted that when you have a folder selected in the manager and you add a new folder it won't put the new folder underneath your selection as you might expect. Instead it will be added directly to the root.

The Import button is what you use to add your individual snippets. If you choose Import, and select your snippet, a new dialog will open up and let you place it in your directory structure. I used the Import button to add our new IsNumeric.snippet. We can see it under the My Code Snippets directory in the Code Snippet Manager. (Listing 1)

The remove button removes the folder from the manager; it doesn't, however, remove it from your system. Also note that the snippets you see in the manager are the ones you'll be able to insert using the Intellisense Snippet Picker.

And finally, the Search Online button takes you to an MSDN search text box where you can search for snippets by keyword. However, I've had more success using Google to find snippets instead.

Other Tools
If you're interested in creating your own snippets, and you want to use a tool with more power than Notepad, check out the Visual Basic Snippet Editor tool located at http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor/. This is a shared source initiative project that's currently in RC mode. This tool is essentially a UI application that lets you create snippets, add and modify parameters like title, author, and description, but in a more user-friendly GUI way. It also lets you preview your snippet as it will appear in the IDE and validates for any compilation errors that may exist. A similar tool doesn't exist yet for C#. Now, there's a project that's screaming to be developed.

Security Considerations
Security considerations are an important issue since snippets contain both source code and hyperlinks. You needn't worry about snippets that come packaged in VS - unless they're modified of course. However, downloaded snippets have potential problems. For one, a file with a .snippet suffix doesn't necessarily mean that it's plain text XML. These files should be scanned with virus protection just like any other downloaded file.

The snippet code itself could be malicious. Fortunately, it's source code and you can read it, but if you are unsure of its origin, look it over carefully. Another problem that may arise is malicious code lurking in collapsed Region tags. You insert the snippet, run it, and your system is damaged because of code you didn't see.

As we discussed above, snippets may contain references that are automatically added to your project when the snippet is inserted. This may reference a file that was downloaded alongside you snippet. This could cause damage as well.

Finally, the HelpURL link in the snippet could execute a malicious script on a separate site, or link the user to an offensive site. It's wise to read the snippet's XML before installing it.

Building a Snippet Library
I hope that I've given you direction in building your own personal library, or perhaps, beginning a library for your company or organization. Having a personalized snippet library helps decrease development time and maintain coding standards. My aim is to get you started; you can decide how a snippet library can help you and your organization.

More Stories By Tommy Newcomb

Tommy Newcomb works for Magenic as an IT consultant in the Chicago area.
His main focus is developing Web application and E-commerce work using Microsoft technologies. He lives with his wife, Emily, and baby daughter, Jaqueline, in the Chicago suburbs.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.