March 02, 2008

Visual Studio Code Snippets

Recently I have been asked by multiple people what rapid design tools I use, or what types of code generation tools do I use.  Many people are surprised when I tell them that for the most part I don't use any rapid generation tools.  I do use a few home grown tools, such as my Info Class Generator, which is available on this site.  However, that is as far as I go with automated code generators.  However, one thing that I have started using more and more frequently are Code Snippets within Visual Studio.

I have found that creating a few helpful Code Snippets has allowed me to quickly and easily format my code, and build my needed structures in almost no time.  In this blog article I will share two of my most commonly used Code Snippets, if there is a demand I will post future code snippets here as well.

Info Class Snippet

One of the most common actions that I perform on a regular basis is the creation of information object classes.  These are classes that have three basic sections to them; private data members, constructors, and public properties.  This snippet is a very basic snippet that just structures a class for me with the standard regions already setup, I just have to drop in the specifics and am ready to go.  The shortcut for this cnippet is "ioclass" and if you type that followed by a tab Visual Studio will insert the following code.

///<summary>
///
///</summary>
public class MyClass
{
    
#region Private Data Members

    
#endregion

    #region
 Public Constructors
    
///<summary>
    /// Default code constructor
    ///</summary>
    
public MyClass()
    {
        
//Initalize any needed values

    
}
    
#endregion

    #region
 Public Properties

    
#endregion
}

I find this very helpful as I can create classes with the exact same regions every time.

C# Property Bodies

The second snippet that I use on a regular basis is a snippet that will automatically form the body of a C# public property, I have 3 auto fill regions setup in the snippet, the first for the data type, the second for the public name, and the third for the private members name.  This allows me to create a public get and set property without typing a single curly brace or return key.  Typically under 10 keystrokes.  The default text of this snippet is below, the access key for this snippet is pbody.

/// <summary>
///
/// </summary>
public int YourMember
{
    
get return _yourMember}
    
set { _yourMember = value; }
}

This is another basic snippet but for those that use C# on a regular basis you will know just how helpful this code is.

Downloads

For those of you that might find these helpful I have decided to make the .snippet files available for download, you will need to unzip the file and put them in the proper location on your system for Visual Studio to find them successfully. If you have any questions, please feel free to use my forum. You may visit the "Code Snippets" page on this site to download the files. You must be a registered user of this site to view the downloads section

Please share any comments below.

tags: ASP.NET, Announcements, .NET 1.1, .NET 2.0, .NET 3.5, C#
comments powered by Disqus

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Content Copyright

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.