In an early warning I described how you crapper create your possess cipher snippets, run them and append them using Visual Studio. The warning piece contained noise cipher for the base IDisposable pattern. Using the piece the whole ornament could be additional to a C# collection enter with a whatever keystrokes.
Replacements
Snippets crapper be prefabricated more multipurpose with the ingest of replacements. These are titled placeholders that are highlighted within the cipher when the piece is inserted. You crapper ingest the journalism key to move between the placeholders and overtype the choice values. This is multipurpose when a piece includes a uncertain or member study that you module poverty to change. The aforementioned agent exact haw be utilised binary nowadays for much an item, with every instances existence replaced with the written text.
An warning of a accepted piece with replacements is utilised to create a newborn concept definition. If you identify “prop” within a C# cipher enter and then advise the journalism key twice, a concept with a choice identify and study is inserted. You crapper journalism between these elements and change the obloquy easily. NB: In Visual Studio 2005 the concept also includes a championship accumulation uncertain that crapper be renamed.
In this article we module create a newborn piece that inserts the base cipher for an circumstance that is supported upon the EventHandler delegate. I module not vindicate the principle of creating or registering the snippet, as this was awninged in the early article.
Creating the Snippet File
We module move by creating the XML for a piece that includes the base layout for the circumstance without placeholders. Add the mass to a newborn 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>
<Description>Code for an circumstance supported upon
the EventHandler delegate.</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[public circumstance EventHandler MyEvent;
open realistic vacuum OnMyEvent()
{
if (MyEvent != null)
MyEvent(this, EventArgs.Empty);
}]]></Code>
</Snippet>
</CodeSnippet>
If you were to run the above snippet, inserting it by typewriting “ehevent” and imperative the journalism key twice would add the mass code:
public circumstance EventHandler MyEvent;
public realistic vacuum OnMyEvent()
{
if (MyEvent != null)
MyEvent(this, EventArgs.Empty);
}
The piece would be multipurpose but the denotive is poor. Ideally we would same a agent that crapper be replaced wherever the text, “MyEvent” is present, including in the study of the “OnMyEvent” method. To attain this we crapper create a replacement.
A equal consists of a titled literal, with a choice value, and whatever placeholders within the code. Any sort of literals haw be circumscribed within the Declarations surroundings of the XML, which is additional exclusive the Snippet surroundings before the Code tag. Each exact includes a unequalled ID, a choice continuance and an nonmandatory tooltip. When you append the piece into your cipher apiece agent is automatically presented the choice value.
We exclusive requirement digit exact papers for the circumstance sample. To tell it with an ID of “EventName”, a tooltip of “Enter the circumstance name” and a choice continuance of “MyEvent”, add the mass XML beneath the inaugural Snippet tag:
<Declarations>
<Literal>
<ID>EventName</ID>
<ToolTip>Enter the circumstance name</ToolTip>
<Default>MyEvent</Default>
</Literal>
</Declarations>
To take where a exact module be inserted you add the exact study to the code, enclosed with note symbols ($). Ours module be proclaimed as “$EventName$”. We requirement to add quaternary placeholders, as follows:
<![CDATA[public circumstance EventHandler $EventName$;
public realistic vacuum On$EventName$()
{
if ($EventName$ != null)
$EventName$(this, EventArgs.Empty);
}]]>