356Microsoft Visual Studio 2010: A Beginner’s Guide

fill in data items when filling in the snippet template in VS. The $end$ variable defines where the cursor ends up after the snippet is complete (after pressing ENTER in the snippet template). You’ll want to locate $end$ where a developer would normally continue typing. The $selected$ variable is used with SurroundsWith snippets, defining the relationship of selected text with where snippet code should be.

Now that you have a basic familiarity with snippets, the next section brings you to the next level as you actually create a new snippet.

Creating New Snippets

To create a new snippet, you can either work from an existing snippet file or start from scratch. If you work from an existing snippet, find and open the snippet closest to what you want to do, using the techniques described in the preceding section. Starting from scratch, there is a quick way to get started using a snippet snippet; that’s right, there is a snippet that helps you create new snippets.

As you learned in the preceding section, snippets are defined as XML files. Fortunately, VS has a nice XML editor that supports XML snippets. So, when I say that we’re going to create a snippet from scratch, that’s not quite true, because we’re going to leverage VS to get a quick start. In the following steps, I’ll show you how to create a snippet you can use to add a C# method to a class quickly:

1.With VS open, press CTRL-Nand create a new XML file. If you were opening the file from an existing project, you would need to provide a name, which would be meth

.snippet. The new XML file has a single line, which is called an XML prefix.

2.Move to the line below the XML prefix, press CTRL-K-X, type sn to select Snippet in the Intellisense list, and press ENTER. You’ll see an XML snippet template with the values for Title, Author, Shortcut, Description, ID, and Default.

3.Fill in data and tab through the snippet template as follows: Title as Method Snippet, Author as <your name>, Shortcut as meth, Description as Create a New Method, ID as access, and Default as public. Press ENTER when complete.

4.The resulting snippet still needs code and template item definitions, which is accomplished by filling in the Code element and adding needed Literal elements. First, modify the code element as follows:

<Code Language="csharp">

<![CDATA[$access$ $return$ $methodName$($paramList$)

{

$end$

}

]]>

</Code>

Page 379
Image 379
Microsoft 9GD00001 manual Creating New Snippets