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:

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
    }
}

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

        #region Member Variables
        String mMemberString;
        int mMemberInt;
        #endregion

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.

<?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"><!&#91;CDATA&#91;public $type$ $property$ { get; set; }$end$&#93;&#93;>
			</code>
		</snippet>
	</codeSnippet>
</codeSnippets>

Change it to be like this:

<?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"><!&#91;CDATA&#91;public $type$ $property$
		{
    			get { return _$property$; }
    			set { _$property$ = value; }
		}
$end$&#93;&#93;>
			</code>
		</snippet>
	</codeSnippet>
</codeSnippets>

The key section that fixes this is:

			<code Language="csharp"><!&#91;CDATA&#91;public $type$ $property$
		{
    			get { return _$property$; }
    			set { _$property$ = value; }
		}
$end$&#93;&#93;>

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

			<code Language="csharp"><!&#91;CDATA&#91;public $type$ $property$
		{
    			get { return m$property$; }
    			set { m$property$ = value; }
		}
$end$&#93;&#93;>

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.