Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Code Reuse with Snippets in Visual Studio 2005
By Paul Kimmel

Rate This Article: Add This Article To:

Code Reuse with Snippets in Visual Studio 2005
( Page 1 of 3 )

Code snippets, stored as XML files, are grown-up versions of the Snippets you already know in today's Visual Studio. Learn how to take advantage of code snippets, which support replaceable values, referencing assemblies, and inserting using statements.

I love to code. I love to write code more than I like to attend meetings, draw UML models, paint GUIs, or anything else. Unfortunately, to get paid well for writing code, it helps to be fast; and there is nothing faster than using code that is known to work and already exists.

Visual Studio is packed full of code that already exists: project and item templates, samples, controls and components, and the .NET Framework. Visual Studio .NET supports copying code right from the code editor into the toolbox; these sections of code copied to the toolbox are called snippets.

As with so many other things, Visual Studio 2005 enhances what we have: snippets grow up into code snippets. Code snippets are as easy to use as snippets were, in earlier versions of .NET, but they're as powerful as project templates. Code snippets, stored as XML in .snippet files, support replaceable values, referencing assemblies, and inserting using statements. The updates to code snippets mean that when you insert a code snippet in Visual Studio 2005, all of the dependencies are added, too.

Beta 2 has no code snippet editor, but you can easily create the snippets as XML text while you wait for the editor; or, you can download a sample snippet editor. (The sample snippet editor comes with source code.) Rather than tell you how to use a snippet editor, which is reasonably straightforward, in this article I show you how to write the XML and insert a snippet the old fashioned way: from scratch.

Snippets 101

Let me get this out of the way. I appreciate XML, but I don't like it. It is ugly, and all of those non-alpha characters make it hard to read and write. That said, the reason snippets are stored as XML is because it is a text-based, an extensible standard; since it's text, it moves easily across the Internet. Transportability is important, because code snippets are intended to be shared by developers.

Tip: Remember, XML is case sensitive.

As XML, all code snippets have a basic format. A stub of this format is shown in listing 1, and you can use this basic snippet (or create a Code Snippet snippet) for every subsequent snippet.

Listing 1: The basic template for every Code Snippet.

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title></Title>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

All you need for a basic code snippet is to add some text between the <Title></Title> tags, and some literal code in the CDATA attribute. The title is used to display in the Intellisense (more on this in a minute), and the text in CDATA[texthere] is placed into a module within the IDE. To create a complete code snippet, I added a title and some code (see Listing 2).

Listing 2: A Code Snippet that finds and highlights some selected text.

<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Searches for text in a TextBox</Title>
    </Header>
    <Snippet>
      <Code Language="CSharp"><![CDATA[
public void Find(TextBox control, string textToFind, int start)
{
  int index = control.Text.IndexOf(textToFind, start);
  if( index == -1 ) return;
  control.Focus();
  control.Select( index, 0 );
  control.ScrollToCaret();
  control.Select(index, textToFind.Length);
}
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Tip: The Language attribute of the <Code> tag has to match the language in which your snippet is written.

In the above sample, you need a reference to System.Windows.Forms.dll, System.dll, and a using statement for the System.Windows.Forms namespace. We'll talk about how to add those elements next.

For the selection in a TextBox to work correctly, especially in a multi-line TextBox with a lot of text, the statements need to be in the correct order. Once I got this order correct — Focus control, locate the caret, scroll the control, and then select the found text — I didn't want to have to figure it out again, so I created the above snippet. When the snippet in Listing 2 is invoked, the code in bold is placed at the current cursor location in the code editor.

Referencing and Importing Assemblies

Any complex snippet needs specific assembly references and using (or imports) statements. The XML for these elements is straightforward. To add references and imports, add a References tag and an Imports tag immediately after the Snippet tag. For each assembly you reference, add a <Reference><Assembly> tag pair; and for each import statement, add an <Import> <Namespace> tag pair. An excerpted sample is shown in Listing 3.

Listing 3: The elided example shows the placement of the imports and references elements in a snippet.

...
    </Header>
      <Snippet>
        <References>
          <Reference>
            <Assembly>System.dll</Assembly>
            <Assembly>System.Windows.Forms.dll</Assembly>
          </Reference>
        </References>
      <Imports>
        <Import>
          <Namespace>System.Windows.Forms</Namespace>
        </Import>
      </Imports>
...

If you insert the above sample in a snippet, the System.dll and System.Windows.Forms.dll assemblies is referenced in the project. In addition, a using statement (for C#, or Imports for VB.NET) introducing the System.Windows.Forms namespace is inserted in the module in which the snippet is inserted.



 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Paul Kimmel