Create Your Own Snippets
Now you know how to retrieve and insert snippets into your projects using the Visual Studio Snippets Manager, and you've probably explored the large number of built-in snippets that ship with Visual Studio. But you're probably eager to create your own snippets and use them the same way. Here's how to create a .snippet
file, save it in the My Code Snippets
folder and use it in your current project. Afterwards, you will see how to add other snippet folders to the Snippets Manager to help organize your code better or access snippets on a network share.
First, create a stub private function called replaceLastOcurr()
that returns a string. Your code should look like this.
Private Function replaceLastOcurr() As String
A snippet file is a well-formed and valid XML file with a .snippet
extension. Start by creating a new XML file to hold the snippet using the standard document type declaration, including the encoding attribute. You can use Visual Studio, or NotePad, or any text editor; however, I use Altova's XML Spy
for all my XML-based documents.
<?xml version="1.0" encoding="UTF-8"?>
Snippet files contain the following nodes, some of which can contain child nodes. The root element is <CodeSnippets>
, which contains one or more <CodeSnippet>
elements. Each <CodeSnippet>
element contains the snippet code, as well as nodes for ancillary information, such as the snippet title, author, description, and keywords. The <CodeSnippet>
element also includes a shortcut phrase and help URL.
The code structure description begins after the header, in a <Snippet>
element. Each <Snippet>
element has a child <Declarations>
element, which contains a set of <Literal>
nodes. A literal
is the description of a replaceable parameter or variable.
- An <ID> element, which is your replaceable parameter.
- A <Type> element, which holds the data type of your replaceable parameter.
- A <ToolTip> element, which displays information or instructions in the Snippets Manager for display on a <MouseOver> event.
- A <Default> element, which is a default value for this literal.
The following and final child element in the <Snippet>
section is <Code>
, which is where your actual code snippet goes. The <Code>
node has two attributes; Language
. The Language
attribute defines the language used for the code and accepts VB
. The Kind
attribute specifies the kind of code the snippet contains and filters out snippets based on your location in the code. This example uses "method body" and "function body."
You begin your code by containing it within a character data declaration.
tag tells the XML interpreter to not parse the codeto treat it as literal content.
You write the code exactly the same as if it were in your project, except that you place the IDs from your Literal nodes where your replaceable parameters go, delimiting them within your code using dollar ($
|Figure 4. An XML Snippet File: This example snippet file contains code converted from an existing DevX VB.NET tip.|
For this example, I've converted an existing DevX VB Tip
to a code snippet. Figure 4
shows an example snippet XML file and how the code for the VB Tip was transformed.
Save this file with a .snippet
extension into the folder C:\Documents and Settings\\My Documents\Visual Studio 2005\Code Snippets\Visual Basic\My Code Snippets
The VS.NET 2005 Beta 2 installation creates that folder during installation, and should already be included in your Snippets Manager. The Snippets Manager comes with a few options to add snippets or folders of snippets. The Add button gives you the ability to add a new folder and all its contents to the directory tree, whereas the Import button lets you import a single snippet into an existing directory within the Manager. There is a Remove button to remove entire folders from the directory tree, but note that the Remove feature does not let you remove individual snippets. There is a Search Online button, but with VS 2005 not yet in production, there were no snippets to be had online at the time of this writing. Give it time.