You are here:About>Computing & Technology>Visual Basic> Using VB.NET> How To Use (And Create) Code Snippets - Part II
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

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

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Nov 14 2006

Code Your Own Snippets

Part I of this article demonstrated how to use Intellisense Code Snippets. There are over 500 of these quick and easy chunks of code included with Visual Studio. When they cover what you want to do, they're the fastest route to high quality programs.

But what about the cases where they don't cover what you want?

Since the Visual Studio Snippet Manager has a built in tool for finding code snippets on the Web, Microsoft seemed to expect a river of downloadable code snippets to become available. But it just hasn't happened. I have never found a code snippet using the tool in Snippet Manager. (Let me know if you have discovered a great source of code snippets, however!) The bottom line is that the Web just isn't a good source ... yet.

So this leaves you in the position of coding your own if you want more than Microsoft provides "in the box" with Visual Studio. You might want to do this if ...

  • You work on a large project with standard methods for performing certain functions, such as looking up an account in a database.
  • You have created a nifty method for doing something and you just want to make sure that you capture the effort for future projects.
  • You're one of those hyper-organized people who manage to simply be more productive.

That last category represents what I wish was my reason for creating code snippets!

If you do create a great code snippet that you want to share with the world - or if you want to check out the Snippets that have been created so far - the best site so far is GotCodeSnippets.NET.

To explore the process of creating code snippets, we're going to look at what a code snippet actually is. Then we'll look at snippet editors to write the XML for a code snippet. We'll also talk about the Code Snippet Manager tool in Visual Studio. And finally, we'll code a useful snippet and upload it to GotCodeSnippets.NET.

For Visual Basic 2005 Express users, I have good news and bad news. The good news is that you can insert and use code snippets with the same ease and benefit that Visual Studio 2005 users enjoy. The bad news is that support for authoring code snippets in their native XML isn't quite as easy using VBE, but it's still possible. So it isn't terrible news, just a little less than you might hope for.

The place to start in understanding the technology is to see what a code snippet actually is. The best way to do this is to locate one of the .snippet files on your system and then drag and drop the file into Internet Explorer. To continue the example in Part I, search for GetARandomNumber.snippet in your Program Files directory. (It's easier than trying to browse to the right directory.) What you should see in IE will look like this:

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

The actual VB.NET code is found in a relatively small part of the XML document near the bottom:

<![CDATA[
Dim generator As New Random
Dim randomValue As Integer
randomValue = generator.Next($LowerBound$, $UpperBound$)
]]>

An XML document like this is what you will create when you code your own code snippet. The format of this document is also checked by an XML schema for accuracy. If you're interested in seeing the real schema, search your Program Files directory for the file name snippetformat.xsd. But your best bet in understanding the schema is to use the Microsoft MSDN Code Snippet schema documentation. If you plan on writing the XML for a code snippet from scratch, this is your best resource to understand how to do it.

Fortunately, you don't have to code the XML directly. There are at least three code snippet editors that you can download. (It's interesting to note that there seems to be more editors available than good code snippets.) To introduce all three, let's use what is probably the most simple code snippet that you can code, the traditional "Hello World" example:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets
  xmlns=
  "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Hello World
      </Title>
    </Header>
    <Snippet>
      <References>
        <Reference>
          <Assembly>System.Windows.Forms.dll</Assembly>
        </Reference>
      </References>
      <Code Language="VB">
        <![CDATA[MessageBox.Show("Hello World")]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

If you copy this to Notepad, save it with a .snippet extension, and drag it into a VB.NET code window, you'll get just the code added to your program:

MessageBox.Show("Hello World")

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.