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

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

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Jul 8 2007

The "other" reusable code base

Intellisense Code Snippets are chunks of code that you can quickly plug into your Visual Basic projects but which also have added features supported by Visual Studio and Visual Basic .NET Express. Note that I used the full title: Intellisense Code Snippets. Microsoft made the unfortunate choice of naming this technology using the same word, "snippet" - a few lines of code selected from a larger program - that programmers have been using for decades for something quite different. So if you're searching for more information, you will have to trudge through lots of pages that have nothing to do with this new technology. In this article, I use the name "Code Snippet" to mean this new Microsoft technology, not just a few lines of code.

Code Snippets feature much more than just lines of code. They're a complete and well designed system that ...

  • Contains replacement fields so the code can be quickly adapted to a specific program
  • Allows them to take advantage of the rest of Intellisense for suggesting potential replacement choices for fields in the code
  • Automatically adds associated references and imports statements (C# doesn't support this feature, however)
  • Displays tooltip instructions to make the code easier to use
  • Organizes these chunks of code into functional groups
  • Is supported by the Code Snippet Manager for maintaining the library of snippets on your computer
  • Is also supported by several choices of snippet editors so you can build your own code snippets - more about this later

That's a lot of features!

Visual Basic 2005 Pro includes over 500 code snippets and VBE has over 200. (According to the Microsoft team, they use the "russian doll" approach: more expensive versions are supersets of the cheaper ones. You're supposed to be able to download more from the web and the Code Snippet Manager even includes a built-in button for doing that. I think Microsoft imagined that developers would start posting free snippets on web sites like crazy but that just hasn't happened. I only found a few sites that had any at all and there were just a handful of snippets in those.

Microsoft isn't giving up, however! Although I failed to find much in the way of third party Code Snippets, I did find Code Snippets that Microsoft has already provided for the unreleased 2007 Office system. These Code Snippets performed functions like:

  • reading/writing headers and footers (Word)
  • adding/reordering slides in a presentation (PowerPoint)
  • retrieving hidden worksheets or hidden rows/columns (Excel)

This should give you confidence that your investment in learning about Code Snippets will be paid back as you work with future Microsoft technologies.

Although there aren't a lot of downloadable Code Snippets available, you can create your own either by hand using raw XML or using one of several alternative Code Snippet editors. (Curiously, it seems there are more editors available than downloadable snippets.) We'll check out both of these approaches in Part II of this article.

But the first task is to gain some experience just using Code Snippets so we can tell what they are. To do that, see the next page to to revisit the ever popular "Drinking Binge" project that was created to demonstrate how to code events in VB.NET.

 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.