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.
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.
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.
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.
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."