Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Give the Visual Studio Snippet Format a Whirl with 10 Pre-packaged Snippets

You aren't limited to using the snippets that ship with Visual Studio .NET 2005; you can convert your own. As a start, here are two in-depth examples showing how to convert existing tip code to snippets, as well as eight other useful tips—already "snippetized" and ready for you to use.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Date: 1/31/2018 @ 2 p.m. ET

n the first article of this two-part Visual Studio Snippets series, I introduced snippets, how to use them, and how to create your own, as well as packaging them for distribution. This time, I'll show you some real-world examples by taking 10 existing code clips from DevX and FreeVBCode and converting them to snippets that you can use in Visual Studio 2005; for the first two, I'll walk you through the conversion process.

As a brief review, snippet files are simply well formed and valid XML files with a .snippet extension stored in a location that must be registered with the Snippet Manager in Visual Studio (VS). To distribute snippets, you must include an XML manifest file with a .vscontent extension that describes the snippet. Finally, you zip both files into a ZIP archive with the extension changed to .vsi so that the Visual Studio Community Content Installer will process the snippets correctly.

You can create your snippets in any XML IDE. Both MSDN and GotDotNet have a beta-ready Visual Basic .NET snippet editor that you can download here. Note the download is available only as a collection of source files, so you will have to compile it yourself unless you don't want to actually install it. I've also included a blank template snippet file (template_snippet.snippet) in the downloadable code that accompanies this article. You can use that template to get started.

Snippetizing Your Code
Before getting started, you need to understand that you must complete the header elements for each snippet so that the Snippets Manager applet will recognize and display your snippets properly. The header elements for each snippet include a title, author, a description, keywords for future searching features, and a "shortcut phrase" (which is one way to insert a snippet into your code). There is also a helpURL element that points to any online documentation available for a snippet.

The beginning of your snippets file should look like this, with each element's value changed appropriately for your particular snippet.

<?xml version="1.0" encoding="UTF-8"?> <CodeSnippets xmlns= "http://schemas.microsoft.com/VisualStudio/ 2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>Replace Last Occurence of a String</Title> <Author>Francesco Balena -- Snippetized by Michael Sanchez</Author> <Description>Replaces only the LAST occurrence of the SEARCH substring in the SOURCE. </Description> <Keywords> <Keyword>"replace"</Keyword> <Keyword>"string manipulation"</Keyword> </Keywords> <Shortcut>replLastStr</Shortcut> <HelpUrl>Help not available</HelpUrl> </Header>

Tip 1: Replacing the Last Occurrence of a Substring
As the first example tip, the following code (see the first article for more detail) replaces the last occurrence of a given string with a different string. You can place this snippet into a function that would accept three arguments; source, search and a replace string.

The original code I am putting into the snippet is,

Dim ReplaceLast As String ReplaceLast = StrReverse( _ Replace(StrReverse(Source), _ StrReverse(Search), _ StrReverse(ReplaceStr), , 1))

You can find the original tip on DevX here.

Creating Replaceable Parameters
After creating the header, you'll need to make alterations to parameterized methods so that your snippets support variable parameters. So, to turn this into a snippet, begin by adding an opening <snippet> element followed by an opening <declarations> element. This block of code accepts three parameters, so you'll need three corresponding <literal></literal> nodes.

The first variable is "Source"—the string to search in, so create an <ID> element with a value of Source. This value represents the highlighted replaceable parameter in the snippet and Visual Studio will insert it into the actual code with dollar signs ($). Next, create a <Type> element with a value of String, which simply declares the data type for this replaceable parameter. Then create a <ToolTip> element with a value of "Replace with the string to be checked." This tool tip appears when users place the mouse pointer over the respective parameter. Finally, add a <Default> element of "Visit VB2TheMax.com and VB2TheMax.com." This is a default value that VS inserts into my code wherever it the value of the <ID> element appears.

Your literal node should look like the following,

<Literal> <ID>Source</ID> <Type>String</Type> <ToolTip>Replace with the string to be checked.</ToolTip> <Default>"Visit VB2TheMax.com and VB2TheMax.com"</Default> </Literal>

The next variable parameter is "Search" which is the string to search for. Create a new <Literal> node to hold the parameter, and inside it, place:

  • An <ID> element with a value of Search. Again, this value represents the highlighted replaceable parameter in the snippet and VS inserts it into the actual code with dollar signs.
  • A <Type> element with a value of String to declare the data type for this replaceable parameter
  • A <ToolTip> element with a descriptive value. I used "Replace with the string to be searched for."
  • A <Default> element and value—I've used "VB2TheMax."
This second <Literal> node should now look like this:

<Literal> <ID>Search</ID> <Type>String</Type> <ToolTip>Replace with the string to be searched for.</ToolTip> <Default>"VB2TheMax"</Default> </Literal>

Repeat the process with the final "replaceStr" variable, which is the string that should replace the Search variable. This third and final <Literal> node should look like the following:

<Literal> <ID>ReplaceStr</ID> <Type>String</Type> <ToolTip>Replace with the string that replaces the string searched for.</ToolTip> <Default>"DevX"</Default> </Literal>

With the three <Literal> nodes completed, close the <Declarations> element and begin the <Code> element. This is where you place the actual code, embedding the value of the <ID> elements in place of the variables, and delimiting them with dollar signs ($).

Within the <Code> element, add two attributes. First, the Language attribute with a value of "VB," and then the Kind attribute with a value of "function body." These attributes control when the snippet shows up in the Intellisense list. For example, if you were working in a C# project, and press CTL+K+X to display snippet Intellisense, this particular snippet would not appear, because the language is defined as VB.

The <Code> element contains your snippet source code, but because source code often includes characters that aren't valid with XML, you need to wrap the code in a CDATA declaration, for example:

<![CDATA[ code_goes_here ]]>

The CDATA tag tells the XML interpreter to not parse the code—to treat it as literal content. After creating the opening CDATA tag, paste in your code and replace the Source variable with $Source$, the Search variable with $Search$ and finally the ReplaceStr variable with $ReplaceStr$. This tells Visual Studio to replace those delimited items with the value of your <Default> element, and to highlight them as replaceable parameters.

Your Code element should look like this:

<Code Language="VB" Kind="function body"> <![CDATA[Dim ReplaceLast As String ReplaceLast = StrReverse( _ Replace(StrReverse($Source$), _ StrReverse($Search$), _ StrReverse($ReplaceStr$), , 1)) Return (ReplaceLast) ]]> </Code>

After completing the <Code> element, close your <Snippet>, <Snippets>, and <CodeSnippet> elements to complete the file. Your snippet should now look like Listing 1:

Save this file with a .snippet extension into the following folder, where <user name> is your user name.

C:\Documents and Settings\<user_name>\My Documents\Visual Studio 2005\Code Snippets\Visual Basic\My Code Snippets.

You can actually store your snippets wherever you like, but you must register that folder with the Snippets Manager. You can do this by pressing CTL+K+B to open the manager, and then clicking the Add button to add a new folder.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date