February 18, 2019
Hot Topics:

Create and Share Your Own Code Snippets

  • January 18, 2006
  • By Josh Fitzgerald
  • Send Email »
  • More Articles »

Writing Your Snippet

First, insert the standard XML that begins the .snippet file:

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">

Next, define your header. The <Title> element is the only child element that is required, but this example includes all of the child elements just to be complete:

  <Author>Josh Fitzgerald</Author>
  <Description>A standard Function/Sub comment header</Description>
  <Title>Function/Sub Comment Header</Title>

Now comes the snippet code definition. First, begin the snippet using the <Snippet> element and then define your <Literal> elements. These will be the pieces of your comment header that will be editable by the user. You'll have a total of four fields, one each for the function/sub name, the function/sub description, the programmer name, and the date:

      <Tooltip>The name of the function/sub</Tooltip>
      <Default>Replace with the Function/Sub Name</Default>
      <Tooltip>A description of what actions the function/sub performs</Tooltip>
      <Default>Replace with a description of this function/sub/</Default>
      <Tooltip>The name of the programmer who created this function/sub</Tooltip>
      <Default>Replace with your name</Default>
      <Tooltip>The date this function/sub was created</Tooltip>
      <Default>Replace with the creation date YYYY-MM-DD format</Default>

The last piece of your snippet file is the code definition. This is where you will define the format of your comment header and specify where the editable fields should be:

<Code Kind="any" Language="VB">
  <![CDATA[' ------------------------------------------------------------------------------
' NAME : $Name$
' DESCRIPTION : $Description$
' PROGRAMMER : $Programmer$
' DATE : $Date$
' ------------------------------------------------------------------------------]]>

Finally, you insert the closing tags to finish up your snippet:


Sharing Your Snippet

Once you've completed your snippet, you'll probably want to share it with your friends and co-workers. You could just email or give them a copy of the .snippet file, but then they would have to locate their snippet folders and manually copy the file.

Wouldn't it be nice if you could make a nice installer that would let them choose where to store the snippet and then copy it there for them? Well, actually, you can package your snippet for easier distribution. You can create a .vsi package (a .zip file with the extension renamed to .vsi) that contains one or more snippets. The .vsi file contains a .snippet file and a config file with a .vscontent extension. The .vscontent file is just an XML file that describes the contents of the .vsi package.

The .vscontent file for your .vsi package will look like this:

<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
    <DisplayName>Function/Sub Comment Header</DisplayName>
    <FileContentType>Code Snippet</FileContentType>
      <Attribute name="lang" value="vb"/>

With your two files, CommentHeader.snippet and CommentHeader.vscontent, in hand, you can create a standard .zip file containing them. Once you create the zip file, you just need to change the extension from .zip to .vsi.

The Snippet Editor mentioned earlier also will create .vsi files for you. All you have to do is select your snippet and click a button.

A Snippet in Time Saves...

You have explored the snippet format top to bottom. You've learned to build your own snippets and how to package them for easy distribution. Now, all that remains is to go forth and create all the snippets you can imagine.

Download the Code

To download the accompanying source code for the example, click here.

About the Author

Josh Fitzgerald is an applications development group leader for a large medical device company in Warsaw, Indiana. Designing and developing Visual Basic .NET applications is only one of his responsibilities, but it is his favorite part of his job. You can reach Josh at josh.fitzgerald@gmail.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date