This note refers to Visual Studio snippets and shows how you can easily add your own. Very useful when having to create many classes with lots of properties such snippets save a lot of time.
They are usually to be found in the Visual Studio folder under My Documents (i.e. for VS 2008 : C:\Documents and Settings\[CrtUser]\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets). The files have extension .snippet and are selectable in VS as long as they are saved in this location , otherwise they have to be added through the Tools menu in the Code Snippet Manager.
Further two examples for getter & setter properties one in the simpler version, the other for List type of values with validation attributes.
The “<Title>Getter Setter String Snippet</Title>” is what will be selectable in the snippets list;
For each replaceable value a set of tags of type “Literal” are needed. This example only replaces the entrance in the “PrivateVar“:
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>Getter Setter String Snippet</Title> <Shortcut>GetterSetterString</Shortcut> <Description>Code snippet for getter and setter methods given the private var name.</Description> <Author>XXX</Author> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>PrivateVar</ID> <ToolTip>Private variable name</ToolTip> <Default>PrivateVar</Default> <Type>string</Type> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ /// <summary> /// $end$ /// </summary> private string _$PrivateVar$;$selected$ public string $PrivateVar$ { get{return this._$PrivateVar$;} set{this._$PrivateVar$ = value ;} } ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
The second example has 3 replaceable values, pressing TAB will jump to the other replaceable value. Pressing ENTER will jump out of the snippet on the position of the $end$ tag :
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>Set Property List Snippet</Title> <Shortcut>SetPropertyList</Shortcut> <Description> Code snippet for setting property value for list. </Description> <Author>XXX</Author> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>PrivateLen</ID> <ToolTip>Length of field</ToolTip> <Default>PrivateLen</Default> <Type>int</Type> </Literal> <Literal Editable="true"> <ID>PrivateVar</ID> <ToolTip>Private variable name</ToolTip> <Default>PrivateVar</Default> <Type>string</Type> </Literal> <Literal Editable="true"> <ID>PrivateType</ID> <ToolTip>Private type in List</ToolTip> <Default>PrivateType</Default> <Type>string</Type> </Literal> </Declarations> <Code Language="CSharp"> <![CDATA[ [MaxLength($PrivateLen$, "Maximum $PrivateLen$ items in list allowed!")] public List<$PrivateType$> $PrivateVar$$selected$ { get { return this._$PrivateVar$; } set { this._$PrivateVar$ = (List<$PrivateType$>)SetProperty(this, "$PrivateVar$", value); } } $end$ ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Welcome back, Sanda – been missing you!