You might think of Visual Studio 2005 code snippets as text replacement tool, but apparently it's more than that.

Check this out: type switch and press tab  - as expected, you get this:

switch (switch_on)
{
  default:
}

The bold parts is for you to fill in. What do think will happen after  you fill in the value? In my case, I typed the name of the local variable of type SeekOrigin. And then I got this:

switch (origin)
{
  case SeekOrigin.Begin:
    break;
  case SeekOrigin.Current:
    break;
  case SeekOrigin.End:
    break;
  default:
    break;
}

Visual Studio knew I was using an enum and filled in all available values! Of course, when you think of it, this makes perfect sense - not many types have limited pool of available values to use in this scenario, but when they do you'd expect to get exactly this behavior.

How is this implemented? Well, I looked into the snippet definition (small XML file whose source is distributed with Visual Studio) and found that the expansion is done by a function call to GenerateSwitchCases($expression$) where $expression$ is the thing you type in (I haven't looked yet where this function resides).

Having the ability to call functions from code snippets makes them a lot more useful and definitely more than just a text replacement tool.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
0 Comments