This is a mirror of official site: http://jasper-net.blogspot.com/

Creating Code Snippets with Replacements

| Sunday, February 27, 2011
Code Snippets
In an earlier example I described how you can create your own code snippets, register them and insert them using Visual Studio. The example snippet contained static code for the basic IDisposable pattern. Using the snippet the entire pattern could be added to a C# class file with a few keystrokes.

Replacements

Snippets can be made more useful with the use of replacements. These are named placeholders that are highlighted within the code when the snippet is inserted. You can use the tab key to jump between the placeholders and overtype the default values. This is useful when a snippet includes a variable or member name that you will want to change. The same placeholder literal may be used multiple times for such an item, with all instances being replaced with the typed text.

An example of a standard snippet with replacements is used to create a new property definition. If you type "prop" within a C# code file and then press the tab key twice, a property with a default type and name is inserted. You can tab between these elements and replace the names easily. NB: In Visual Studio 2005 the property also includes a backing store variable that can be renamed.
In this article we will create a new snippet that inserts the basic code for an event that is based upon the EventHandler delegate. I will not explain the basics of creating or registering the snippet, as this was covered in the earlier article.

Creating the Snippet File

We will start by creating the XML for a snippet that includes the basic layout for the event without placeholders. Add the following to a new XML file:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet
   Format="1.0.0"
   xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <Header>
       <Title>Create Event</Title>
       <Author>BlackWasp</Author>
       <Shortcut>ehevent</Shortcut>

Read more: Black Wasp

Posted via email from Jasper-net

0 comments: