Recipe 2.2. Creating a New Snippet


Problem

You've written an especially useful block of source code, and you want to save it as a code snippet for use in other applications.

Solution

To save a block of code as a snippet for reuse, use the Code Snippet Editor for Visual Basic 2005 to create and store the snippet, or hand-code the required XML file yourself and place it in an appropriate directory.

Discussion

To add a new snippet to the set of available snippets in your Visual Studio environment, fire up the Snippet Editor, and right-click on the folder where you want the snippet to appear, then select Add New Snippet from the shortcut menu.(An Add New Folder option lets you adjust the available folders. You can also create subordinate folders to a reasonable depth.) Type or paste your code into the blank pane of the Editor tab, using the Replacements tab to add any replaceable parameters. Click the Save icon near the top of the Snippet Editor to store your results. The new snippet will be available immediately within Visual Studio.

The Snippet Editor is a community-developed application available to you as a free download. You can contribute features to it yourself if you are so inclined. It's part of the "GotDotNet" Community, located at http://www.gotdotnet.com, in its "Workspaces" area.

It's also possible to code snippets yourself, using the markup specified by the Microsoft XML snippet schema. However, doing so is not for the faint of heart, and with few exceptions, the Snippet Editor is more than adequate.


Figure 2-3 shows the "Convert a Number to a Hexadecimal String" snippet used earlier, as presented in the Snippet Editor.

You are probably dying to see the actual XML that makes up a code snippet, so here is the XML for that snippet:

 <?xml version="1.0" encoding="UTF-8"?> <CodeSnippets xmlns=   "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">   <CodeSnippet Format="1.0.0">     <Header>       <Title>Convert a Number to a Hexadecimal String</Title>       <Author>Microsoft Corporation</Author>       <Description>Returns the hexadecimal         representation of an integer.</Description>       <Shortcut>typeHex</Shortcut>     </Header>     <Snippet>       <Imports>         <Import>           <Namespace>System</Namespace>         </Import>         <Import>           <Namespace>Microsoft.VisualBasic</Namespace>         </Import>       </Imports>       <Declarations>         <Literal>           <ID>Number</ID>           <Type />           <ToolTip>Replace with an integer.</ToolTip>           <Default>48</Default>           <Function />         </Literal>       </Declarations>       <Code Language="VB" Kind="method body">         <![CDATA[Dim hexString As String = Hex($Number$)]]>       </Code>     </Snippet>   </CodeSnippet> </CodeSnippets> 

Figure 2-3. The Snippet Editor


You can find this particular block of snippet code at C:\Program Files\Microsoft Visual Studio 8\VB\ Snippets\1033\datatypes\ConvertaNumbertoaHexaDecimalString.snippet. It's pretty easy to read, although a pain to write. That's why we have software applications like the Snippet Editor.

See Also

Recipes 2.1, 2.3, and 2.4 provide additional information on code snippets.

If you really feel that you must write your own snippets and use all the advanced features available within the XML schema, Microsoft's MSDN Library has full documentation on using and designing snippets.Link to http://msdn.microsoft.com, and search for "snippet."




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net