ode reuse has been a mantra of software development for decades. .NET provides for code reuse in many ways including inheritance, components, and of course the indispensable function. Visual Studio 2003 expands the concept of reuse by providing a set of editable built-in templates for any project items, such as Windows Forms, Web Forms, or classes. Templates let you define the structure and content of your project items and reuse them each time you add an item to your project. To gain the full productivity benefits, you can create your own templates and tailor them to your specific needs.
Editing a Built-In Template
is a pattern with a custom format that is used as a starting point for building applications. Visual Studio provides a set of built-in templates for forms, classes, and other project items. When you add a class to a project, for example, Visual Studio uses a class template to provide you with the starting point for the class.
But often the built-in templates don't provide all of the functionality that you want. In Visual Studio, you can edit the built-in templates or create custom templates.
You can find the majority of the built-in templates in your \Program Files\Microsoft Visual Studio .NET 2003
directory. The VB templates are in the Vb7\VBWizards
subdirectory and the C# templates are in the VC#\VC#Wizards
Each type of item, such as Windows Forms, Web Forms, and classes, has its own folder under the appropriate VBWizards or VC#Wizards subdirectory. You can navigate down to an existing template file to edit any of the built-in templates.
For example, to edit the standard class template for VB, navigate to \Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\Class\Templates\ 1033\Class.vb. Open this file and add any code or comments that you wish to include in the built-in template. A common change is to include Option Strict On in the standard VB class template as follows:
Option Strict On
Public Class [!output SAFE_ITEM_NAME]
For C#, navigate to \Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\ CSharpAddClassWiz\Templates\1033\NewCSharpFile.cs. Open this file and add any code or comments that you wish to include in the template. A common change is to include additional type imports in the standard C# class template as follows:
namespace [!output SAFE_NAMESPACE_NAME]
/// Summary description for [!output
public class [!output SAFE_CLASS_NAME]
public [!output SAFE_CLASS_NAME]()
// TODO: Add constructor logic here
Notice the [!output SAFE_ITEM_NAME]
syntax in both of these examples. The Visual Studio wizard uses these directives to substitute the name you provide when you add a project item using the Add New Item dialog box.
Once you have updated a template file, save it and give it a try. Open any exiting project or create a new project. Right-click on the project and select Add then choose Add Class from the context menu. Give the class a name and click OK. Visual Studio creates the class file using your revised template. Notice that Visual Studio substitutes the class name you specified into the appropriate locations in the template as defined by the template directives.