Posts tagged ‘Snippet’

Changing the prop snippet for creating a Property in C#

Ok, so it is very common for the c# member variables to start with either an _ (underscore) or an m.  So when creating a property, you can save a lot of time by changing it to assume this as well.

For example, your class may look as follows:

[sourcecode language=”xml”]
namespace AgentConfigurationPlugin
{
public class Class1
{
#region Member Variables
String _MemberString;
int _MemberInt;
#endregion

#region Constructors

/*
* The default constructor
*/
public Class1()
{
}

#endregion

#region Properties
public String MemberString
{
get { return _MemberString; }
set { _MemberString = value; }
}

public int Memberint
{
get { return _MemberInt; }
set { _MemberInt = value; }
}
#endregion
}
}
[/sourcecode]

Note: I use the _ character even though it is hard to type (being up to the right of my pinky finger), so if you prefer, use the letter “m”, which is easy to type (being just below my pointer finger) and it also stands for “member variable”.

[sourcecode language=”xml”]
#region Member Variables
String mMemberString;
int mMemberInt;
#endregion
[/sourcecode]

Anyway, whether it is an “m” or “_” or any other character, it is common to prefix member variables. So it would be useful if the property snippet assumed that prefix character as well.

The default snippet for creating a Property is located here:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#\prop.snippet

The contents looks as follows.

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for an automatically implemented property</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public $type$ $property$ { get; set; }$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
[/sourcecode]

Change it to be like this:

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>prop</Title>
<Shortcut>prop</Shortcut>
<Description>Code snippet for an automatically implemented property</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public $type$ $property$
{
get { return _$property$; }
set { _$property$ = value; }
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
[/sourcecode]

The key section that fixes this is:

[sourcecode language=”xml”]
<Code Language="csharp"><![CDATA[public $type$ $property$
{
get { return _$property$; }
set { _$property$ = value; }
}
$end$]]>
[/sourcecode]

Or if you use “m” instead of “_” as I do, of course you would replace the “_” with an “m”.

[sourcecode language=”xml”]
<Code Language="csharp"><![CDATA[public $type$ $property$
{
get { return m$property$; }
set { m$property$ = value; }
}
$end$]]>
[/sourcecode]

Now when you create a member variable and then a property that matches it exactly except for the prefix character, the works is done for you, making you a more efficient programmer.

You may want to change the propg snippet as well.


Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.