Jon Webb's Blog

Friday, February 02, 2007

Customize Class Item in Visual Studio 2005

I wanted to customize the Visual Basic Class template in Visual Studio 2005 so it automatically added headers etc. I ran into problems, but eventually found a way around them, and haven't found any other answers on the web.
The first thing I tried was to create a new item template following the instructions in the Visual Studio template help at http://msdn2.microsoft.com/en-us/library/ms247069(VS.80).aspx.
I created a file called Class.vb, with these contents:

' $safeitemname$
'
' Written by Jon A. Webb 2007
' Copyright (c) 2007 by Jon A. Webb
' All right reserved.
'
' $History: $
'
'
Public $safeitemname$ $safeitemname$
#Region "$safeitemname$ variables"
#End Region
#Region "$safeitemname$ lifecycle"
#End Region
#Region "$safeitemname$ operations"
#End Region
#Region "$safeitemname$ properties"
#End Region
End $safeitemname$
Then I selected File->Export template and followed the instructions there to add the template, naming it 'Class'.
This worked, in part. When I added a class called 'Class1' to the project I got this:
' Class18
'
' Written by Jon A. Webb 2007
' Copyright (c) 2007 by Jon A. Webb
' All right reserved.
'
' $History: $
'
'
Public Class18 Class18
#Region "Class18 variables"
#End Region
#Region "Class18 lifecycle"
#End Region
#Region "Class18 operations"
#End Region
#Region "Class18 properties"
#End Region
End Class18
Oops -- at one point, doing a global search and replace, I'd changed 'class' everywhere to '$safeitemname'. That resulted in the class keyword getting replaced. What I'd actually meant to use as the template was
' $safeitemname$
'
' Written by Jon A. Webb 2007
' Copyright (c) 2007 by Jon A. Webb
' All right reserved.
'
' $History: $
'
'
Public Class $safeitemname$
#Region "$safeitemname$ variables"
#End Region
#Region "$safeitemname$ lifecycle"
#End Region
#Region "$safeitemname$ operations"
#End Region
#Region "$safeitemname$ properties"
#End Region
End Class
OK, now, how do I fix this? I tried re-exporting the template but nothing changed. Add new class still used the old template. Then I tried following the instructions at http://geekswithblogs.net/ehammersley/archive/2005/11/08/59451.aspx. I ended up doing
devenv /installvstemplates
Still nothing -- the old template was still there. I poked around and found the cache Visual Studio was using for the template at C:\Documents and Settings\webb\Application Data\Microsoft\VisualStudio\8.0\ItemTemplatesCache. Clearing the directory and rerunning the devenv command above had no effect. There must be a second level cache somewhere.
OK, so, poking around, I found another cache at C:\Documents and Settings\webb\My Documents\Visual Studio 2005\Templates\ItemTemplates with another copy of the old Class.zip there. Deleting it, too, gave the original behavior when I added a class -- an empty class without my customizations. But then re-exporting the template gave me back the template with "Class" globally substituted by $safeitemname$. Very weird. Where was the template coming from? Nowhere -- I searched the disk. Then I realized what was going on... Visual Studio 2005 was itself substituting Class with $safeitemname$. In other words, if I submitted this as a template:
' Class
'
' Written by Jon A. Webb 2007
' Copyright (c) 2007 by Jon A. Webb
' All right reserved.
'
' $History: $
'
'
Public Class Class
#Region "Class variables"
#End Region
#Region "Class lifecycle"
#End Region
#Region "Class operations"
#End Region
#Region "Class properties"
#End Region
End Class
I'd end up with the template above with $safeitemname$ everywhere. I tried this, and it happened. But that leads to a solution -- use capitalization to get around this. So the final template was
' Class
'
' Written by Jon A. Webb 2007
' Copyright (c) 2007 by Jon A. Webb
' All right reserved.
'
' $History: $
'
'
Public class Class
#Region "Class variables"
#End Region
#Region "Class lifecycle"
#End Region
#Region "Class operations"
#End Region
#Region "Class properties"
#End Region
End class
(Note: I had to edit and save this file with a text editor, not Visual Studio, to keep the final 'class' from getting automatically capitalized.) Exporting this as a template worked!

Labels: , ,

0 Comments:

Post a Comment

<< Home