Contents
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...
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?
using System;
using System.Diagnostics;
namespace Wizardry1
{
public class Class1
{
#region Class1 fields
#endregion
#region Class1 properties
#endregion
#region Class1 constructors
public Class1()
{
}
#endregion
#region Class1 public members
#endregion
#region Class1 private members
#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:
using System;
namespace [!output SAFE_NAMESPACE_NAME]
{
public class [!output SAFE_CLASS_NAME]
{
public [!output SAFE_CLASS_NAME]()
{
}
}
}
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:
using System;
namespace [!output SAFE_NAMESPACE_NAME]
{
public class [!output SAFE_CLASS_NAME]
{
#region [!output SAFE_CLASS_NAME] fields
#endregion
#region [!output SAFE_CLASS_NAME] properties
#endregion
#region [!output SAFE_CLASS_NAME] constructors
public [!output SAFE_CLASS_NAME]()
{
}
#endregion
#region [!output SAFE_CLASS_NAME] public members
#endregion
#region [!output SAFE_CLASS_NAME] private members
#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:
using System;
namespace Wizardry1
{
public class MyClass
{
#region MyClass fields
#endregion
#region MyClass properties
#endregion
#region MyClass constructors
public MyClass()
{
}
#endregion
#region MyClass public members
#endregion
#region MyClass private members
#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:
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:
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:
function OnFinish(selProj, selObj)
{
var strSafeProjectName = CreateSafeName(strProjectName);
wizard.AddSymbol("SAFE_PROJECT_NAME", strSafeProjectName);
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:
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.