Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Maximize Your Productivity with Project Item Templates : Page 2

Create your own Windows Forms, Web Forms, or class templates to reuse code and maximize your productivity in Visual Studio 2003.


advertisement
Creating a Project Item Template
As you develop or enhance applications, you will find that there are common elements in most of the Windows Forms, Web Forms, or business objects that you create. For example, you may find that each of the code files for your business objects contains a standard comment header, a common list of imports, a usual definition of regions, and a predefined set of interfaces or method signatures.
Templates are also great for team development efforts because they promote consistency in how the code is structured.
Each time you add a business object to your application, you could create all of the common elements again or go looking for a similar class and use the copy and paste method of reuse—but that is not very efficient. It is better to build a template that contains all of the reusable code and comments and then use it as a starting point each time you need a new business object.

The ability to edit the built-in templates is cool—and you could edit a built-in template to incorporate all of your custom needs. But you may want to keep your existing built-in templates simple and create new, special-purpose templates for more advanced requirements. Creating your own templates for Windows Forms, Web Forms, classes, or other project items involves five basic steps:

  1. Create a wizard directory that will contain your template and the supporting files that Visual Studio will need to process your template.
  2. Create the template in the appropriate wizard directory.
  3. Update the other supporting files in the wizard directory as needed.
  4. Add a wizard file for your template that specifies your wizard directory and other wizard parameters.
  5. Add data about your template, including the wizard file name, to the Visual Studio directory (VSDir) file used by the Add New Item dialog box.
After you finish this process, your template will appear in the Add New Item dialog box as shown in Figure 1. When you select your template from the Add New Item dialog box, Visual Studio will use the associated wizard file to execute the wizard and add a project item to your project using your template. All of code and comments that you put into your template will automatically be included in the new project item, giving you all of the common code you need before you even begin.
 
Figure 1: The template appears in the Add New Item dialog box.
Let's go through this process in more detail by creating a class project item template for a custom business object (BO) both in VB and in C#.


Step 1: Creating a Wizard Directory
Following with the general reuse theme of this article, instead of creating a template and associated directories from scratch, use an existing template as a starting point. Start by locating the wizard subdirectory containing the template closest to the template you wish to create and copy the entire subdirectory. It is important to copy the entire subdirectory because you will need all the associated supporting files in order for your new template to work correctly within Visual Studio.

For this example, copy either the VB or C# class template. In VB, copy the entire \Program Files\Microsoft Visual Studio .NET 2003\Vb7\VBWizards\Class directory. In C#, copy the entire \Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\CsharpAddClassWiz directory. Rename the copied directory to any name you choose for your template wizard, such as BOTemplateWiz. Step 2: Creating the Template
Since the wizard directory for the new template is a copy of an existing wizard directory, it already contains a template file. In the Templates\1033 subdirectory under the newly renamed BOTemplateWiz directory, change the name of the template file to any desired name. In this example, the VB file is changed to BOTemplate.vb and change the C# file to BOTemplate.cs.

For now, don't bother adding any code or comments to the template. Let's continue with the remaining steps to make this template work within Visual Studio—then we will add all of our custom code.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date