Tuesday, March 31, 2009

Defaulting a class to public from Visual Studio

Something that's been niggling at me over the years that I've used Visual Studio is that when a class is created in the IDE it's always created with no public access modifier and so defaults to internal access. E.g.
class Class1
{
}
What I want is the class to be created as such:
public class Class1
{
}
And I know that I'm not alone because when I ask my co-workers they agree. I asked this question on StackOverlow and was pointed in the right direction. On my 64 bit machine I still haven't solved this problem but on my 32-bit XP machine I have.
I first edited the Class.cs file inside the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip package and added the public modifier to the class.
This didn't work.
I then found this folder: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip and did the same addition on the Class.cs file in this folder and it worked.
I'm not sure if I can blow away this cache version of the ItemTemplates and it would be re-created? I didn't want to try because my development environment is too brittle to mess with at this level. I would guess that at some point the cache (if it is a cache) may become invalidated and so the first edit will be necessary and prevail in the long term.

1 comment:

  1. You were correct in editing C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip. The next step is to open a Visual Studio command shell and execute the following command to updated the ItemTemplatesCache (which you modified directly above):
    devenv /installvstemplates

    ReplyDelete