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 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”.
#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"><![CDATA[public $type$ $property$ { get; set; }$end$]]> </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"><![CDATA[public $type$ $property$ { get { return _$property$; } set { _$property$ = value; } } $end$]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
The key section that fixes this is:
<Code Language="csharp"><![CDATA[public $type$ $property$ { get { return _$property$; } set { _$property$ = value; } } $end$]]>
Or if you use “m” instead of “_” as I do, of course you would replace the “_” with an “m”.
<Code Language="csharp"><![CDATA[public $type$ $property$ { get { return m$property$; } set { m$property$ = value; } } $end$]]>
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.
[...] prop snippet for creating a Property in C# Filed under: FreeBSD — rhyous @ 12:25 pm Read more Share this:DiggRedditLike this:LikeBe the first to like this post. Leave a [...]
This is an antiquated and unnecessary coding style. Unless it's 1997, no member variables should be preceeded by an underscore, or even worse, an 'm' or any other symbol.
Thank you for sharing your opinion. I somewhat agree that some of these things are annoying. The only problem is that many companies still have standards to code that way. This is a very common coding style requirement in many companies today. So I would call it mainstream because it is still coded that way in the main stream.
Hi, I recently had the same idea but this solution has one drawback. Since fields should be named starting with lower case e.g. _memberString I would like to have either the first character of the field name converted to lower case after the ledaing "_" or the first character of the property name converted to upper case automatically. Any clues about this?
Ok, so you are saying that you have a suggest syntax that says:
1. The first character of a Field must be an underscore "_"
2. The second character must be lowercase?
I am sorry that there is no way for the snippet tool to accomplish that rule.
I wish I knew how to do what you want, but it is not a feature of Visual Studio currently.
You could always have your Fields start with _m such as _mSomeVariable.
However, I am going to suggest that you should drop the second stipulation in the above suggested syntax.
Is there a reason that prefixing a field with an underscore is not enough?
So given the following for values:
_FirstName
_LastName
FirstName
LastName
Are you saying that because the character after the underscore is capitalized, that you cannot determine which are fields and which are properties.