Creating a Code Snippet:
To create a .snippet file
1. On the File menu in Visual Studio, click New and then click File.
2. Click XML File and then click Open.
3. On the File menu, click Save
4. In the Save as type box, select All Files (*.*).
5. In the File name box, enter a file name with the .snippet file name extension.
6. Click Save.
A line is automatically generated in the file. Below the automatically generated line of XML, add a CodeSnippets element with the proper xmlns attribute value, and a CodeSnippet element to create an individual code snippet.
Following is a code snippet which will provide the set/get property. The Default value is string.
Note: Please remove the '#' before using this code snippet file. Somehow, this blog is not allowing tags [I am thinking of finding ways to make it allow tags and will post it in a separate blog article :)]
<#?xml version="1.0" encoding="utf-8"?>
<#CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<#CodeSnippet Format="1.0.0">
<#Header>
<#Title>Get/Set Property<#/Title>
<#Shortcut$>getset<#/Shortcut$>
<#Description>Code snippet for an automatically implemented property with 'get' and 'set'<#/Description>
<#Author>Vinoth<#/Author>
<#SnippetTypes>
<#SnippetType>Expansion<#/SnippetType>
<#/SnippetTypes>
<#/Header$>
<#Snippet>
<#Declarations>
<#Literal>
<#ID>type<#/ID>
<#ToolTip>Property Type<#/ToolTip>
<#Default>string<#/Default>
<#/Literal>
<#Literal>
<#ID>property<#/ID>
<#ToolTip>Property Name<#/ToolTip>
<#Default>MyProperty<#/Default>
<#/Literal>
<#/Declarations>
<#Code Language="csharp">
<#![CDATA[
private $type$ _$property$;
public $type$ $property$
{
get{ return _$property$;}
set{ _$property$ = value;}
}
$end$
]]>
<#/Code>
<#/Snippet>
<#/CodeSnippet>
<#/CodeSnippets>
This file should be added to "My Code Snippets" folder of Visual Studio. It will be automatically detected and will be included in the intellisense Reflection.
The default path is
"C:\Documents and Settings\{user}\My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets"
Using code snippet in the code behind file:
To use the code snippet one has to type the shortcut value specified in the header section of the code snippet.
for the above given code snippet, the shortcut value is "getset".
User has to type this keyword and press the "Tab" key twice.
This will insert the following code
private string _MyProperty;
public string MyProperty
{
get { return _MyProperty; }
set { _MyProperty = value; }
}
No comments:
Post a Comment