• Code Snippets revisited

    By in Code Snippets on

    In my last post concerning Code Snippets I covered some of the basics. This post will cover some details i didn’t mention before.

    Today I will talk about:

    • If a single Code Snippet can support different programming languages
    • How to write a SurroundsWith-Snippet
    • Why the SnippetType should be set
    • Code Snippets Functions
    • NUnit Code Snippets for Visual Studio 2005

    Is it possible to create Code Snippets supporting different programming languages?

    When creating your own Snippets, you normally do this for a specific language e.g. C#. But what if you got a team where some guys code in C#, some in VB.NET and you would like to create only one set of snippets each supporting code for C# AND VB.NET in a single file?

    Unfortunately I have to say, that it doesn’t work.


    Even though the XML schema for defining Code Snippets allows you to define more than one Code-tag inside a Snippet definition, Visual Studio 2005 only parses a Snippet for the first existing Code-tag and ignores all others.

    How to write a SurroundsWith-Snippet

    When you select a part of your code and right-click, you’ll find a Surround With entry, which actually lists all Snippets supporting this mode.

    Preparing a Snippet for surrounding the current selection is fairly simple: You just have to contain the $selected$ parameter at the designated position as you can see in the example of the #region Snippet, which is shipped with Visual Studio 2005.


    The SnippetType should be set to SurroundsWith (see next chapter).

    Why should the SnippetType be set?

    The type of a Snippet can be defined using the SnippetType-tag inside the Snippet header.


    It’s an optional tag, but it should always be set due to the following reasons.

    There are three values, which can be used inside the SnippetType-tag:

    • SurroundsWith: allows the code snippet to be placed around a selected piece of code.
    • Expansion: allows the code snippet to be inserted at the cursor.
    • Refactoring: specifies that the code snippet is used during Visual C# refactoring. Refactoring cannot be used in custom code snippets.

    As you can see, only the first two types can be used for defining own Snippets. But those two are quite important.

    The type Expansion is used for inserting the code of a Snippet at the current cursor position e.g. when using a snippet for inserting a property. It overwrites the current selection!

    So, if you select a part of your code, without having set the SnippetType correctly, this Snippet will also be presented when you right-click in your code and select Surround with. If you then choose a snippet, which isn’t prepared for actually surrounding your selected code with something, your code will be overwritten on insertion of the Snippet code.

    As you can see in the #region example, it is allowed to define multiple SnippetType-tags.

    Code Snippets Functions

    When defining a Snippet parameter (see Literal-tag) you can specify a Function-tag. You cannot define own functions, but there are 4 predefined functions you can use for C# Code Snippets.

    • GenerateSwitchCases(EnumerationLiteral)
    • ClassName()
    • SimpleTypeName(TypeName)
    • CallBase(Parameter)

    For a description of those functions please take a look at the function definitions in the MSDN. Examples on how to use this functions can also be found there.

    NUnit Code Snippets for Visual Studio 2005

    Scott Bellware published a great set of Code Snippets for working with NUnit. You can find them here.

    Read more
  • Just trust me, OK?

    By in General on

    I don’t know, if you heard about the Thawte Web Of Trust:

    The thawte Web of Trust (WOT) is a Certification system that allows your identity to be validated for use in your Personal Certificate.

    I am telling you this, because as of now I am a WOT notary myself and can therefore give away trust points to others.

    If you would like to get you own e-mail signing certificate with your name included, just contact me so I can give you some trust points. (50 points are needed to get your personal certificate.)

    I hear you thinking: “Free e-mail certificate you say? Where’s the catch?” Well there is a little catch as always: You have to stand before me in person and show me your passport, so I can identify yourself.

    So, if you are willing to come to Bonn/Germany, you are welcome to visit me 😉

    Read more
  • Code Snippets – the whole enchilada

    By in Code Snippets on

    If you are using Visual Studio 2005, you sure have encountered a new nifty feature called Code Snippets.

    There are a lot of articles and blog posts surrounding this topic, so why another post? Well I’ll try to give give you a (hopefully) complete roundup on

    • what Code Snippets are
    • how you can use them
    • how to create your own
    • where to find some already done by others
    • and how to install and manage them

    I’ll describe the handling of Code Snippets in C#. In VB.NET Code Snippets behave a bit different, but since I am only using C#, I can’t tell you much about that.

    Introduction to Code Snippets

    Code Snippets are short pieces of reusable code for everyday use, which can be used in a very handy way.

    Well, since Visual Studio 2003 you can write own pieces of code, which you can drag to the Toolbox for reuse, but using Code Snippets is A LOT sweeter.

    You don’t have to use the mouse to drag them to your workplace anymore. Just type in the defined shortcut and press the TAB-key two times to insert a snippet. For browsing available Code Snippets you just press Ctrl+K,X or select Edit – IntelliSense – Insert Snippet… in the Visual Studio menu.

    Also, you can define placeholders inside a snippet, which are replaced during insertion. You can take a look at a nice demo and how to by Jeff Atwood.

    Create your own Code Snippets

    Code Snippets are simply plain XML files, so you could write them in notepad with a little help of the Code Snippets Schema Reference. I would recommend taking a look at Snippy. Snippy is a free editor for creating and editing Code Snippets. Since the tools hosted on gotdotnet aren’t usually well documented, you might want to take a look at the overview at Visual Studio Hacks.

    Ready made Code Snippets by other develolpers

    There are some good locations on the web, where you can find ready to use snippets made by other developers.

    First I’d like to point you to the snippets done by my colleague Roland. He did some for inserting Properties with Prefix Notation, Events, NUnit Test Methods, etc.

    After that you should take a look at GotCodeSnippets.NET, where many user defined Code Snippets are hosted.

    In VB.NET you find a big bunch of already installed Code Snippets for any circumstance. Microsoft just published the same snippets for C# as a free download. Stupidly the installation isn’t really that, what you want. In the C# Snippet Parity post by Jeff Atwood, you find a better way of installing, which brings me to the last section of this article.

    Managing Code Snippets

    Since Jeff Atwood wrote nearly everything I planned to write in his C# Snippet Parity post ;), I try to make this short.

    Jeff wrote about a way to install the additional C# Code Snippets by Microsoft using a system wide approach and a registry file. I prefer a per user installation by the following reasons

    • Normally I am the only one on my PC coding
    • I don’t like to install “personal” things in the Program Files folder, because they are gone after a clean system installation. (I use a different partition to store my files on, so I never have to worry when reinstalling my system.)
    • The snippet paths are stored in the HKCU path in the registry anyway
    • I don’t have to add new snippet paths to the registry

    So, what to do? When installing Visual Studio 2005, the path Visual Studio 2005Code SnippetsVisual C#My Code Snippets will automatically be created inside the My Documents folder.

    You can simply download the repackaged C# Code Snippets by Jeff and unpack them into the My Code Snippets folder. When calling the Visual Studio 2005 Code Snippets Manager using Ctrl+K,B or Tools – Code Snippets Manager…, you should find the new snippets inside the My Code Snippets folder.



    You can simply reorganize your Code Snippets using the Windows Explorer. Now, how easy is that?

    UPDATE: Jeff just wrote a nice macro for enumerating all installed Code Snippets.

    Just a few installation hints, since some guys had problems with it:

    • Download it from here
    • Unpack and rename it to KeyboardShortcuts.vb
    • Start the Macros IDE (Alt+F11 out of Visual Studio)
    • Import it by right-clicking on MyMacros -> Add – Add Existing Item

    The rest should be clear. If not, just take a look at Jeff’s post.

    Links in this post (in order of appearance)

    This article is also filed here.

    Read more
  • Multi-Touch Interaction – new ways to communicate with computers?

    By in Gadgets on

    The guys from Multi-Touch Interaction Research have published an awesome video showing new ways to interact with computer systems.

    You have to see this video!

    Read more
  • Bonn-to-Code.Net – We have a lift-off (non-english post)

    By in .NET on

    Gestern Abend war das erste Treffen der lokalen .NET User Group Bonn-to-Code.Net.

    Mit 14 Codern war das Ganze schon ein voller Erfolg. Es war ein bunt gemischter Kreis, der sicherlich einige spannende Themen behandeln wird.

    Gestern ging es erst einmal darum, sich gegenseitg etwas besser kennenzulernen. Zum nÀchsten Termin am 21.03. wird es dann die ersten VortrÀge geben. Um welches Thema/Themen es genau gehen wird, ist noch in der KlÀrung.

    Solltet Ihr Interesse haben Euch in lockerer Runde mit .NET Themen zu beschÀftigen, schaut doch einfach einmal vorbei! Gebt vorher am Besten Roland bescheid, damit wir etwas besser planen können.

    Read more