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):

  <CodeSnippet Format="1.0.0">
      <Title>WebPart Connection Methods</Title>
          <ToolTip>Describe the connection</ToolTip>

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 system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus


  • Visual Studio 'Tea & Technology' Video Miniseries Starts Next Week

    The goal of the miniseries is to provide an insider's snapshot look at some of the people who contribute to shaping the Visual Studio IDE every day.

  • Microsoft Releases OpenJDK 21 Build for Java Jockeys

    Microsoft today announced its "Microsoft Build of OpenJDK 21," taking advantage of new features and functionality in Java 21.

  • 'Dev Home' Update Leads Developer Goodies in AI-Powered Windows 11 Update

    Along with today's new AI-powered Windows 11 update come new goodies for developers, including a new edition of Dev Home, a preview offering described as a "control center" providing coding-focused features and functionality.

  • Community Dev Gives VS Code Python Some YAPF

    The latest update to Python in Visual Studio Code includes a new extension for Python formatting that was contributed by a member of the open source community.

Subscribe on YouTube