Monday, May 15, 2006

The previous post was just about the basic principles of code snippets in VS2005.  Now we will describe the power boost of code snippets: the $name$ notation (or the literal replacement) and the object replacement.

The literal replacement.

When you want to create a code snippet with some customized parts (like variable type, variable name, ...) that must replaced by the developer after inserting the code snippet, you can use a literal.

How do you create this?
In the Snippet element place an element called "Declarations".  In this element you place your literals which can be one or more literals.  The literal element has a few elements:

  • ID: The ID of the literal
  • Default: The default value for the literal

Optional you can add:

  • Function: Function to execute when the literal receives focus in Visual Studio
  • Tool tip: short description of the literal

Now you have created your literal, but you have to place it in your code snippet.  This is done by placing the $ sign in front and at the end of your Literal ID element.  Now go to the code element.  Within your code place the $literalID$ notation where you want it.  The next time you add the code snippet you will see a green rectangle.  This is the place where you have added your literal.  If you have inserted a default value this will be filled in the rectangle.

You can have multiple insertions for the same $literalID$ element.  This gives us a nice effect in Visual Studio.  Only for the first entry you will see the green rectangle.  After filling in the rectangle, Visual Studio will automatically replace all the other literals with the same ID with your filled in value.

Two examples:
Example 1: One literal

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>My MessageboxSnippet</Title>
<Shortcut>mmbs</Shortcut>
<Description>My Snippet for messagebox in c#</Description>
<Author>
</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>YourMessage</ID>
<ToolTip>Place your message here</ToolTip>
<Default>Place your message here</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[MessageBox.Show("$YourMessage$");]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Example 2 : More literals and used more than one time

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>My MessageboxSnippet</Title>
<Shortcut>mmbs</Shortcut>
<Description>My Snippet for messagebox in c#</Description>
<Author>
</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>YourMessage</ID>
<ToolTip>Place your message here</ToolTip>
<Default>Place your message here</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Title</ID>
<ToolTip>Place your message title here</ToolTip>
<Default>Place your message title here</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[MessageBox.Show("$YourMessage$","$Title$");
MessageBox.Show("$YourMessage$");]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

As you can see, code snippets in VS2005 give some great power to developers.  I have created or downloaded and changed them to our needs, several good code snippets. Every developer has installed them locally.

5/15/2006 6:08:07 PM (Romance Standard Time, UTC+01:00)  #     |