Visual Studio Tip: Setting Up Code Snippets with Variable Parameters

Importing a snippet into Visual Studio and then using the snippet is a snap.

As near as I can tell, very few developers create their own Visual Studio code snippets. There's a reason for that: the XML you have to wrap around code snippet is a real pain to write.

Fortunately, importing a snippet into Visual Studio and then using the snippet is a snap. So, as a public service, here's the wrapping XML you need to create a code snippet: copy and paste it somewhere safe until you need it.

When I'm creating a WebPart, there's a bunch of code I add in automatically. Here's the code with the only text that changes from one WebPart to another, marked with question marks:

<ConnectionProvider("???", "ConnectionPointIWebpartField", _
        AllowsMultipleConnections:=True)> _
Public Function ???FieldConnection() As IWebPartField
  Return Me
End Function

<ConnectionProvider("???", "ConnectionPointIWebpartRow", _
        AllowsMultipleConnections:=True)> _
Public Function ???RowConnection() As IWebPartRow
        Return Me
End Function

<ConnectionProvider("???", "ConnectionPointIWebpartTable", _
        AllowsMultipleConnections:=True)> _
Public Function ???TableConnection() As IWebPartTable
  Return Me
End Function

What I want to do is set this up as a snippet that will allow me to easily replace the question marks with some text appropriate to the application I'm building. I know: I should create a Visual Studio Project Item template or a base class that I can (somehow) inherit from.

But as a consultant, I move from one client to another, and it's a lot easier to import my own set of code snippet files when I'm on a client's site than get my client to adopt some base object I've created (besides, there's been the odd scenario when I've needed to add this code to a WebPart multiple times).

To create a code snippet, start by opening NotePad and pasting the following XML into it. This base XML, shown in Listing 1, includes a spot for a title, a replacement parameter (to handle those question marks) and a short cut to make it easier to insert the snippet.

After replacing  the parts with the question marks, the top of my WebParts snippet looks like this (if you don't need a replacement parameter, just delete the Declarations element with everything it contains; if you need more than one replacement parameter, copy and paste the Literal element):

<CodeSnippets 
   xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>WebPart Connection Methods</Title>
      <Shortcut>WebPartConns</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>WebPartText</ID>
          <ToolTip>Describe the connection</ToolTip>
          <Default>Data</Default>

Once you've made those changes, drop your code inside the CDATA block, inserting your replaceable parameter (delimited by dollar signs) where you need text replaced. For my WebParts code, the result looks like this:

<ConnectionProvider("$WebPartConn$", "ConnectionPointIWebpartField", _
        AllowsMultipleConnections:=True)> _
    Public Function $WebPartConns$FieldConnection() As IWebPartField
        Return Me
    End Function
… rest of code…

Then save the file with the extension .snippet somewhere that it's easy to find  (I called this one WebPartConns.snippet and saved it to my desktop).

And, yes, Visual Studio does have a code snippet for creating code snippets, but it's more trouble than it's worth.

Now comes the easy part -- importing the snippet into Visual studio: From the Tools menu, select Code Snippets Manager. In the resulting dialog, click the Import button and navigate to your .snippet file. Select the file and click the Open button. The dialog will let you choose which categories you want your snippet added to, but it's automatically added to the My Snippets category, which is probably what you want. Click the Finish button and you've added your snippet to Visual Studio. You can now delete your .snippet file from wherever you saved it; Visual Studio keeps its copy in a safe place.

Using the snippet is equally easy: Type your shortcut (WebPartConns, in my case) into your code file and, in Visual Basic, press the Tab key. Your snippet will be inserted and your mouse will be positioned at the first place your replacement parameter is required (the other places it's needed will be highlighted). Type in your replacement value and press the Tab key again. Your code is ready to go.

About the Author

Peter Vogel is a principal in PH&V Information Services, specializing in Web development with expertise in SOA, client-side development, and user interface design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His most recent book ("rtfm*") is on writing effective user manuals, and his blog on language and technical writing can be found at rtfmphvis.blogspot.com.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.