Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / Javascript
Article

Building VS .NET Wizards - Part 1

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
5 May 20054 min read 50.8K   56   5
Visual Studio .NET already brings us a lot of wizards, to help us create skeleton classes and projects. Developers can extend these templates and even change the existing ones.

Contents

It's a kind of magic

Go on! Run Visual Studio and create a new project in any language you desire, e.g. C#. What happens once you click Ok? It's a kind of magic... Visual Studio creates a new project for you and adds an empty class to that project. Visual Studio brings a lot of nice wizards which help you in developing large projects - but is that enough?

I decided to say No! So get your crystal ball and see the magic reveal...

Introduction

This is the first article of a series of four, I have decided to write. It covers a way to adjust the wizards which are already defined. There are several other resources even here at CodeProject which explains the same subject. I've added them to the References section at the end of this article. One really good article is the "Creating project item code templates for Visual Studio.NET" by Emil Aström, so I don't want to cover the terms described in this article again.

A few things the developers should always do is to document and test their code. Code documentation is done easily with C# using code documentation tags and can even be brought to great readable format using tools such as NDoc. Testing can be done using NUnit or TestDriven.NET which is a great add-in for Visual Studio.

So some steps you would normally do, while creating new classes is to add code comments to your classes, which will include the date, time and author of the class in the <remarks> section of your code comment. This would normally apply to the default constructor which is created for you. Maybe you would also like to add some regions to your class for your fields, properties, constructors and methods?

C#
using System;
using System.Diagnostics;
namespace Wizardry1
{
    /// <summary>
    /// Comments for Class1.
    /// </summary>
    /// <remarks>
    /// <para>created: 5.May 2005</para>
    /// <para>Author  : Michael Groeger</para>
    /// </remarks>
    public class Class1
    {

        #region Class1 fields
        // add private fields here
        #endregion
        #region Class1  properties
        // add properties here
        #endregion
        #region Class1 constructors
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <remarks>
        /// <para>created: 5.May 2005</para>
        /// <para>Author  : Michael Groeger</para>
        /// </remarks>
        public Class1()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #endregion
        #region Class1  public members
        // add public members (properties/methods) here
        #endregion
        #region Class1  private members
        // add private members (properties/methods) here
        #endregion
    }
}

Adjusting the class template

The class template

When you add a new class to your project using the Visual Studio wizard the skeleton class is created from a template, which can be found at, C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\CSharpAddClassWiz\Templates\1033\, called NewCSharpFile.cs. It should look something like this:

C#
using System;
namespace [!output SAFE_NAMESPACE_NAME]
{
    /// <summary>
    /// Zusammenfassung für [!output SAFE_CLASS_NAME].
    /// </summary>
    public class [!output SAFE_CLASS_NAME]
    {
        public [!output SAFE_CLASS_NAME]()
        {
            //
            // TODO: Fügen Sie hier die Konstruktorlogik hinzu
            //
        }
    }
}

Remarks: The subdirectory 1033 has the localized versions of the template for US-English.

SAFE_NAMESPACE_NAME and SAFE_CLASS_NAME are placeholders for the namespace and the class name respectively. These will be replaced by the wizard with the class name you set and the namespace the new class will correspond to. Now we want to adjust the template a little bit.

Adding some code comments

Backup the class template NewCSharpFile.cs and then open a copy of it and save it under NewCSharpFile.cs again. Open the template in Visual Studio and add some comments and region to it as follows:

C#
using System;
namespace [!output SAFE_NAMESPACE_NAME]
{
    /// <summary>
    /// Comments for [!output SAFE_CLASS_NAME]
    /// </summary>
    /// <remarks>
    /// <para>created: [!output CREATION_DATE]/para>
    /// <para>Author  : Your name goes here</para>
    /// </remarks>
    public class [!output SAFE_CLASS_NAME]
    {
 
        #region [!output SAFE_CLASS_NAME] fields
        // add private fields here
        #endregion
        #region [!output SAFE_CLASS_NAME]  properties
        // add properties here
        #endregion
        #region [!output SAFE_CLASS_NAME] constructors
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <remarks>
        /// <para>created: [!output CREATION_DATE]</para>
        /// <para>Author  : Your name goes here</para>
        /// </remarks>
        public [!output SAFE_CLASS_NAME]()
        {
            //
            // Add constructor logic here
            //
        }
        #endregion
        
        #region [!output SAFE_CLASS_NAME]  public members
        
        // add public members (properties/methods) here
        
        #endregion
        
        #region [!output SAFE_CLASS_NAME]  private members
        
        // add private members (properties/methods) here
        
        #endregion                
        
    }
}

Save the file and start another instance of Visual Studio. Open a project and add a new class using the wizard. The class that is generated now, should look like this:

C#
using System;

namespace Wizardry1
{
    /// <summary>
    /// Zusammenfassung fr MyClass.
    /// </summary>
    /// <remarks>
    /// <para>erstellt: [!output CREATION_DATE]</para>
    /// <para>Author  : Michael Groeger</para>
    /// </remarks>
    public class MyClass
    {

        #region MyClass fields

        // add private fields here

        #endregion

        #region MyClass  properties

        // add properties here

        #endregion

        #region MyClass constructors

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <remarks>
        /// <para>erstellt: [!output CREATION_DATE]</para>
        /// <para>Author  : Michael Groeger</para>
        /// </remarks>
        public MyClass()
        {
            //
            // TODO: Fgen Sie hier die Konstruktorlogik hinzu
            //
        }

        #endregion

        #region MyClass  public members

        // add public members (properties/methods) here

        #endregion

        #region MyClass  private members

        // add private members (properties/methods) here

        #endregion

    }
}

So the wizard did a wonderful job for you. But the placeholder CREATION_DATE was not replaced. CREATION_DATE is not part of the parameters the Visual Studio wizard comes along with. So you have to add it on your own.

Adding custom parameters

Beside the class template in the Templates subdirectory there is a JScript file called default.js at, C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\CSharpAddClassWiz\Scripts\1033\.

Again 1033 has the localized versions for US-English. Here is the place where you can add your own parameters to the wizard. Adding parameters to the wizard is simply done by calling:

JavaScript
wizard.AddSymbol([SYMBOL], [VALUE]);

What we do now is to add the logic for adding the current date to the wizards parameters. Backup the JScript file and open it in Visual Studio. The JScript function for adding the date will look like this:

JavaScript
function CreationDate()
{
       var Months = new Array("January","February","March","April","May","June",
                "July","August","September","October","November","December");
    var myDate = new Date();
    var currentDate = myDate.getDate()
    +"."
    + Months[myDate.getMonth()]
    +" "
    +myDate.getFullYear();
    
    wizard.AddSymbol("CREATION_DATE",currentDate);
}

This is enough code to create and add the Date to the parameters of the wizard. All we have to do now is to add a call to our function in the OnFinish() function which is already defined:

JavaScript
function OnFinish(selProj, selObj)
{        
// [...]
    var strSafeProjectName     = CreateSafeName(strProjectName);
    wizard.AddSymbol("SAFE_PROJECT_NAME", strSafeProjectName);

    // add CreationDate() call    
    CreationDate();
    
    SetTargetFullPath(selObj);
    var strProjectPath    = wizard.FindSymbol("TARGET_FULLPATH");
    var strTemplatePath     = wizard.FindSymbol("TEMPLATES_PATH");
// [...]        
}

Save the JScript file and now try to add again a new class to your project using the class wizard. Now CREATION_DATE should be replaced by the current date:

C#
/// <summary>
/// Comments for Class1.
/// </summary>
/// <remarks>
/// <para>created: 5.May 2005</para>
/// <para>Author  : Michael Groeger</para>
/// </remarks>

Summary

I have shown you that it is easy to adjust existing wizards to your own needs, by just doing a few steps. These steps are:

  • edit the C# class template NewCSharpFile.cs.
  • edit the JScript file default.js to add some new parameters.

In the next article I will explain, how you can easily create your own wizards.

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer Groeger Consulting
Germany Germany
Michael Groeger started .NET early 2001 when it was revealed at the Technical Summit in Neuss, Germany. Before that he was developing mostly using Visual C++ 6 on Windows Platforms or with vi/gc++ on several Unix derivates.

Since 2004 Michael Groeger settled off as freelancer and is focused on projects in the financial and insurance sector.

Comments and Discussions

 
GeneralUseful work! Pin
mavel22-Sep-05 2:58
mavel22-Sep-05 2:58 
GeneralDefault.js....not found... Pin
RLyda20-Jul-05 11:17
RLyda20-Jul-05 11:17 
Generalgood idear Pin
lovelydinasour00118-Jul-05 22:39
professionallovelydinasour00118-Jul-05 22:39 
Generalbug fix Pin
lunky8-May-05 5:08
lunky8-May-05 5:08 
GeneralRe: bug fix Pin
Michael Groeger8-May-05 21:50
Michael Groeger8-May-05 21:50 

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.