David Hayden [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Customize Visual Studio 2005 Templates for Coding Productivity

My 2-year-old baby boy is a bit under the weather, so we hung out at home while Mom and the girls enjoyed a number of fall activities we had planned this weekend.  One of the tasks he helped me with was downloading and installing the production bits of Visual Studio 2005, etc. on the test machine.  Our first order of business was to better learn the new IDE and make it bend to our will ;)

 

Visual Studio 2005 Templates

Visual Studio 2005 contains several templates that can be modified for when you add new items to your websites and projects.  In particular, I have always been a bit disappointed by the default Class Template when adding a new C# Class to my projects.  It is quite useless actually, merely providing a default namespace and a minimal class shell named something like Class#.

Visual Studio 2005 has default templates for all the items buried in the %Program Files% directory on your PC.  Luckily for us, we can actually modify these templates if we wish to create a template that is a bit more practical and personable to our liking.  Being a bit of a productivity junkie interested in code generation, O/R Mappers, etc., I decided to play with the Class Template today to make it suit more of my normal Class Environment.

Shown below is a directory tree of the template locations.  Templates are located in *.ZIP files within your ItemTemplates Folder, and a cache of these items are located in the ItemTemplatesCache Folder.  There is also a NewFileItems folder not shown for when you add items via File > New Item.

 

 

Modifying the Class Template

To customize the Class Template, modify the Class.cs file ( or the appropriate one for your preferred language ) with the default code that you would like to appear when a class is intially created.  Add all the normal statements, fields, properties, contructors, etc. to this template that you would normally have to type manually.

The templates do have some variable parameters that can be used.  I haven't dug deep into the list of parameters, but this page will get you started:

http://msdn2.microsoft.com/en-us/library/eehb4faa(en-us,vs.80).aspx

 

I made a few changes to the Class Template as a test to add XML comments; regions for fields, properties, constructors, etc; overrides for ToString(), etc. of Object; and a default constructor.  Later I will fill this out more with a bit more detail.  Shown below is the outlined code view for my practice default template for a C# Class.  Note that all of this is from the customized template.  I typed nothing.

 

 

Conclusion

Code Generation and productivity comes in a number of flavors.  The easiest way to increase productivity in your day-to-day programming is to modify the default templates for Visual Studio 2005.  Later I will show you how to create custom templates as well.

 

DrinkingDragon Well Green Tea

 


Posted Sat, Nov 5 2005 5:38 PM by David Hayden

[Advertisement]

Comments

timh wrote re: Customize Visual Studio 2005 Templates for Coding Productivity
on Sun, Nov 6 2005 1:51 PM
another method (without altering the base files or creating a new template) would be to leverage the intellisense code snippet features.

you could start a new class file out of the box type "myclass" and your custom class would be implemented the same as the template.

just another method using the cool features.

www.gotcodesnippets.net
scottgu@microsoft.com wrote re: Customize Visual Studio 2005 Templates for Coding Productivity
on Sun, Nov 6 2005 4:48 PM
Here is a pointer to a blog entry I did on how to do this for web page templates as well: http://weblogs.asp.net/scottgu/archive/2005/09/09/424780.aspx

Hope this helps,

Scott
David Hayden wrote Creating Custom Item Templates in Visual Studio 2005
on Sun, Nov 6 2005 4:54 PM
In my last post, I talked about How To Customize Existing Visual Studio 2005 Templates for Coding Productivity. ...
Christopher Steen wrote Link Listing - November 6, 2005
on Sun, Nov 6 2005 11:17 PM
ASP.NET 2.0 Url Rewriting crippled to the point of
uselessness [Via: James
Avery ]
Async Pages,...
Devlicio.us