Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

Snippet to Generate a Public Property + Private Backing Field + Documentation Tags inside #regions

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
14 Jan 2008CPOL3 min read 26.4K   118   17   5
Code snippet to generate a property with private backing field, linked documentations tags, inside #regions. Ability to config type, field and property's name and the default init value

Introduction

The problem with the majority of the snippets that I found is that none, absolutely none goes beyond the code generation! Sometimes I found very good snippets, well coded, but finish, nothing more. No generation of documentation, no #regions directives! Yes, I know! Nobody likes documentation, but when you need to explain what the hell your class/method/etc. does to your buddy, or the same explanation needs to be re-made sometime after the code is ready, and you are working on another project, you'll remember the documentation that you did not make!

#regions are another problem. Nobody likes the mix "snippets plus #regions". Inside Visual Studio, #regions enable blocks of code that have affinity to be visually put together, and with the collapse and expansion features, have the ability to focus on different levels of abstractions of the same code.

After this, I did decide to write a code snippet to a common task to every program's day: a property generation snippet. This snippet isn't a big one, far from this! This snippet aims only to show how even a small problem, when well solved can be of great help. ("this is not a big one", this snippet has up-to 46 lines. I've another snippet that generates a class inside your own namespace, with a custom exception class, eventlog helper class, performance counters, dispose methods, and documentation, that generates about 3.000 lines of code!!!)

Background

You can develop a snippet event with text editor or inside Visual Studio, but when you decide to delve in the code snippet world, the first smart thing to do is look for a snippet editor. In my experience, two free snippet editors are worth mentioning: "Snippet Editor" and "SnippetBuilder", the first one is what I use majority of the time.

When you develop snippets with a text editor, you'll need to work directly with the snippet's XML tag, which can be error prone. An editor will be of great help because this tends to hide the details. Provide some level of syntax coloring (very poor! but better than nothing!) and then publish the snippet. A snippet is basically a .xml file, but when you publish the snippet file, it is transformed into a .vsi that you or another person can import directly into Visual Studio only by double-clicking the .vsi file. And the most important feature: the editor is of great help when your snippet enables customization (replacements)! What is the ability to change the contents of the replacement variables with the user desired values at the moment the snippet is activated.

Using the Code

The following code shows the first part of the snippet:

C#
#region _$field$ backing field

#region Documentation

/// <summary>_$field$ is a backing variable to $property$ property. 
/// (Describe better)</summary>
/// <example>
/// 	<code lang="CS" title="." description=".">
/// $type$ $field$ = this._$field$;
///     </code>
/// </example>
/// <remarks>This field is marked with DebuggerBrowsableState.Never attribute.</remarks>
/// <requirements>.</requirements>
/// <seealso cref="$property$" cat="Fronting Property">$property$</seealso>

#endregion Documentation

[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private $type$ _$field$ = $defaultValue$;

#endregion _$field$ backing field

#region $property$ Fronting property

#region Documentation

/// <summary>$property$ of type $type$ (Describe better).</summary>
/// <value>Returns $type$.</value>
/// <exception cref="" caption=""></exception>
/// <example>
/// 	<para>$type$ _$property$ = this.$property$;</para>
/// </example>
/// <remarks>This property is marked with XmlIgnore and 
/// DebuggerStepThrough attributes.</remarks>
/// <requirements>.</requirements>
/// <seealso cref="_$field$" cat="Backing Field">_$field$.</seealso>

#endregion Documentation

[XmlIgnore]
public $type$ $property$
{
   [DebuggerStepThrough] get { return this._$field$;}
   [DebuggerStepThrough] set { this._$field$ = value;}
}

#endregion $property$ Fronting property
$end$	

This block generates a backing variable named: _$field$ of type $type$ with an init default value $defaultValue$, and property named: $property$ of the same type. These replacement variables will be changed to the desired values, by example:

  • $field$..............= workedDays
  • $type$................= int
  • $defaultValue$..= 0
  • $property$.......= WorkedDays

It will produce this:

C#
#region _workedDays backing field

#region Documentation

/// <summary>_workedDays is a backing variable to WorkedDays property.
/// (Describe better)</summary>
/// <example>
///   <code lang="CS" title="." description=".">
/// int workedDays = this._workedDays;
///     </code>
/// </example>
/// <remarks>This field is marked with DebuggerBrowsableState.Never attribute.
/// </remarks>
/// <requirements>.</requirements>
/// <seealso cref="WorkedDays" cat="Fronting Property">WorkedDays</seealso>

#endregion Documentation

[DebuggerBrowsable(DebuggerBrowsableState.Never)] private int _workedDays = 0;

#endregion _workedDays backing field

#region WorkedDays Fronting property

#region Documentation

/// <summary>WorkedDays of type int (Describe better).</summary>
/// <value>Returns int.</value>
/// <exception cref="" caption=""></exception>
/// <example>
///   <para>int _WorkedDays = this.WorkedDays;</para>
/// </example>
/// <remarks>This property is marked with XmlIgnore and
/// DebuggerStepThrough attributes.</remarks>
/// <requirements>.</requirements>
/// <seealso cref="_workedDays" cat="Backing Field">_workedDays.</seealso>

#endregion Documentation

[XmlIgnore]
public int WorkedDays
{
   [DebuggerStepThrough] get { return this._workedDays; }
   [DebuggerStepThrough] set { this._workedDays = value; }
}

#endregion WorkedDays Fronting property

Points of Interest

Beyond the generated code, the documentation with references between the backing field and the property (see the XML tag <seealso>!), and the #regions generated are the main point here.

The documentation is almost ready, with a very little effort on summary tag, this can be finished. Any one that comes to use this code will be able to see the IntelliSense showing the contents of the summary tag. And the #regions will enable to expand/collapse the blocks of code to the desired level.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
Since 1984 in the IT, working with:
• Programming languages
• System Analysis
• Data and Database Administration since 1991
• Consulting and
• Lectures.

occulate@yahoo.com
Melbourne - Australia

Comments and Discussions

 
Suggestionrubish Pin
Asif Ashraf4-Mar-12 1:47
professionalAsif Ashraf4-Mar-12 1:47 
GeneralSnippet Code Pin
Jenifer7-Jan-08 7:07
Jenifer7-Jan-08 7:07 
AnswerRe: Snippet Code Pin
Alex Mello Occulate14-Jan-08 11:32
Alex Mello Occulate14-Jan-08 11:32 
Jenifer, sorry for the delay!

I guess now you can download the prop.zip file, that contains both the .vs and .snippet files, at the link in the begin of the article!


Thank you for your post!


Cheers Big Grin | :-D
GeneralToo much bloat ... Pin
Pop Catalin4-Jan-08 0:12
Pop Catalin4-Jan-08 0:12 
GeneralRe: Too much bloat ... Pin
Ilíon4-Jan-08 4:59
Ilíon4-Jan-08 4:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.