Pages Menu
TwitterRssFacebook

Posted by on Jan 4, 2012 in Development

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:

 

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

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.