Pages Menu
TwitterRssFacebook

Posted by on Jan 4, 2012 in Development | 0 comments

Visual Studio New Class Template Modification

Visual Studio New Class Template Modification

New Year is a good time for some introspection. I’ve been looking at how I can do things, and re-reading the most excellent book The Pragmatic Programmer: From Journeyman to Master.

One of the mantras of a pragmatic programmer is avoiding repetition by using automation.

In VB.NET, creating a new class file usually involves the same things again and again:

  • Setting Option Explicit On
  • Setting Option Strict On
  • Importing some frequently used namespaces
  • Adding some comments about who created the file, and why

The default template provided for VB.NET class files is pretty poor:

Public Class #CLASSNAME#

End Class

Even if we can just add the Options in, it will save a couple of seconds each time. More importantly, it won’t ever be forgotten.

This is easily done in Visual Studio. Within the application folder structure, navigate to /Microsoft Visual Studio 10.0/Common7/IDE/ItemTemplates.

Where you go from here depends slightly on your language. For VB.NET it’s VisualBasic/Code/1033/Class.zip.

Copy and extract the .zip file, and simply edit the code file to contain whatever you want, then carefully zip back up and replace. MSDN has a good list of available template commands which you can use to insert the current user, date etc.

Finally, rebuild the templates for VS start devenv with the flag /installvstemplates

NOTE: don’t use the in-built Windows 7 ZIP File extensions to unzip/zip the zip file, as the file it creates isn’t recognised by Visual Studio, resulting in the modified template not showing at all in the list of types to add! Instead, use something better, like 7zip.

Here’s my new New Class file now:

 

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This site uses cookies to help make this website better. By continuing to use this site we’ll assume you’re OK with that (implied consent).