1. Home
  2. Computing & Technology
  3. Visual Basic

How To Use (And Create) Code Snippets - Part II
Creating Code Snippets

By Dan Mabbutt, About.com

Nov 14 2006

The fact that all of the code snippet editors seem a bit beta-ish makes the option of falling back to editing source XML directly a reasonable option for creating snippets. Fortunately, Microsoft has supplied a snippet for creating snippets too! To use this snippet in Visual Studio 2005, first add an XML file to a project. You can use a normal project or an empty project.

This is where Visual Studio 2005 Express users might have a little more difficulty. There is no XML template in VBE. But there is support for editing an XML file! Add a text file and save it in your project folder as a .XML file to trigger the XML editing.

Visual Studio users will see an editing window with the XML header already in it. (VBE users will have to add this header manually.) The "encoding" attribute is optional.

<?xml version="1.0" encoding="utf-8" ?>

Everyone now has access to the the Snippet snippet. Right-click below the XML header and select Insert Snippet .... (XML files don't seem to support the handy "?-tab" shortcut for inserting code snippets for some reason.)

Here's what the code editor looks like so far (in VBE - VS 2005 is very similar).

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Double click on the Snippet snippet and you get ...

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

This is the basic code snippet with dummy values. After you add your own values, remember to save the file as a ".snippet" file before putting it into production. But it's probably easier to just start with one of the existing snippet files that seems closest to what you want to do and then save it with a new name.

The Code Snippets Manager

If you start looking through the VB.NET menus for snippet-related items, the only thing you will find is the Code Snippets Manager under the Tools menu. This is a handy tool, but it can be very confusing. For example, in her blog, author and guru Michèle Leroux Bustamante, after trying to use the Add ... / Import ... buttons in the Code Snippet Manager wrote, "Aaarrrrghhhh...," "...it was nuts...I was going nuts...maybe I am nuts..."

First, people often expect the Code Snippets Manager to help you create or use Snippets and they get confused when they can't figure out how. It doesn't do that. It only "helps" you manage the files and directories that Snippets are in. The directory location of snippets is a key part of how they work and you must use the Code Snippets Manager (rather than Windows Explorer, for example) or they won't work correctly in Visual Studio or VBE.

Second, the function of the Add ... and Import ... buttons isn't clear at all. The Add ... button brings in a reference to a directory that happens to have snippets in it. It's easy to make a mistake and get two references to the same directory. (A Microsoft guru did exactly that during an online presentation recently.) If that happens, just "Remove" it again. The Remove button only removes the reference. The original directory remains intact. In fact, if you want to actually delete a Snippet file, you have to hunt it down in the actual directory with Windows Explorer. You can use any directory to save snippets, but it has to be "added" using the Code Snippet Manager Add ... button before you can use the VB.NET interface to insert it into your program.

The Import ... button "adds" a snippet into an existing directory that is referenced in the Code Snippet Manager. So, if you have a code snippet somewhere on your hard drive that you want to use in your program, you can "import" it into one of the referenced directories using this button.

The Search Online ... button is, at present, useless. I've never found a usable snippet that way.

The About Visual Basic Word Count Code Snippet

Using a tip submitted by Peter Zilahy Ingerman, PhD, I wrote a short article showing how to use the Split() function in Visual Basic called Amazing Splits. One of the things that can be done using Peter's tip is a word count of a text string. This idea is a real improvement over the way this job is usually done. Most of the time, you get the advice to use a loop to count text. This code avoids all that.

This seemed like a great example of something that could be put into a code snippet so others can benefit. So I fired up the VB Snippet Editor, navigated to the My Code Snippets folder, and right-clicked to bring up the context menu that allows a new snippet to be entered.

The completed forms for the snippet look like this:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Adding the completed snippet into a program is no problem. Here's an example of the how it's done with the complete example showing how the replacement field, TextStringToCount is also integrated into Intellisense.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

To complete the picture, contribute to the common good, and get that warm fuzzy feeling from being a good netizen, I uploaded the completed snippet to GotCodeSnippets.NET. You can download it there!

Don't forget to upload your own code snippet!

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. How To Use (And Create) Code Snippets - Part II

©2009 About.com, a part of The New York Times Company.

All rights reserved.