Create and Export Custom Classes as Reusable Templates in VB .NET


Visual Studio 2005 and single Express Editions implement a new great feature, which allows you to create your own project templates or item templates. These last can be added to your project when you select the "Project/Add new item" menu command.

In this article I will show how to create a new Visual Basic class and export the result as a template to be added to a project via the "Project/Add new item" menu command. An empty class will be integrated with the IDisposable interface implementation. I chose this interface because it's simple to use as an example and it can be very useful when you need to create objects which implement some clean-up code.

This article applies to Visual Basic 2005. I used Visual Basic 2005 Express Edition but the same steps apply to Visual Studio 2005.

We'll create a new empty class, then add the code to implement the IDisposable Interface. In the end we'll export the resulting class to a new template.

Create a new class

When you create a class, depending on the kind of it, you could need to implement the IDisposable interface.

The IDisposable interface provides objects and methods able to "destroy" the instance of the class. A disposable class exposes a "dispose" method, which frees up some memory and releases the object when your application does not need it anymore. Typical examples of disposable classes are Windows Forms.

So it could be useful to create a class file, which provides disposing code by the moment of its creation without writing code manually every time.

Let's begin. Select the "File\New project" menu command. Then, choose "Class Library" and leave unchanged the filename for the new project. In my case project name is "ClassLibrary1", as shown here:


When the project is created, double click on "Class1.vb" file in the Solution Explorer. Go to code and write the following line of code:

Implements IDisposable

as shown here:


Now simply press enter. The IDE will automatically add all the necessary code to expose disposing methods as follows:


And that's all. Comments put by the IDE are useful to understand where you should write the so-called cleanup code for releasing unused resources.

Our class is now ready to be exported as a new template.

Exporting our class to a new template

Before proceeding, save the changes then choose the "File\Export template" menu command. You should see the following window:


You can choose between two options: export as a new project template or as a new item template. Choose the "Item template" option, assure the correct project name is selected then press "Next".

The following window will appear:


Just select "Class1.vb", since other items should be selected only when exporting as project template. Then still click "Next" to see this:


Here you can specify some references for your template, if required. In this case no reference is needed because the code we wrote uses features of the base class library, which is referenced as default by any kind of existing project. Remember that an item template would be added only to existing projects. You're ready to click "Next" to perform the last step:


In this window we can customize our template settings. In the "Template name" textbox replace ClassLibrary1 with IDisposableClassLibrary. In the "Template description" write the following text: "An empty class which implements IDisposable interface". Output location should be leave unchanged, since Visual Studio automatically recognize the right location for exported templates, though you can choose one different.

You can also specify an icon for your template to be displayed in the "Add new item" dialog.

Click "Finish" to complete the process. To see how the new template works, open a project of yours and try to add a new item via the "Project\Add new item" menu command.

You should get an output like this:


Now you'll see how a new "disposable" class is added to your project, ready to be integrated with your code.

Custom templates are usually located in the %My Documents%\Visual Studio 2005\My Templates directory. As you can easily see, each template is made of a zipped archive. Archives contain files formerly created and then exported. This means that you could realize templates also manually, but I think it's a better idea to use the IDE wizard.