When you add a new class or interface to an existing project in Visual Studio.NET 2005, VS.NET 2005 will not define this class (or interface) as public
by default.
In Visual Studio.NET 2003 however, new classes and interfaces always received the public
access modifier by default, and I do like this VS.NET 2003 approach far better.
When I create a class-library, most of the classes and interfaces that are contained in this library are meant to be used outside the library itself. This means that I have to explicitly add the public
access modifier to most of my classes, and this is a dreadfull job.
Not only is it a boring job to manually define this access modifier for (almost) every new class that you create, it sometimes causes me loosing some time as well:
Today, I created a new class in VS.NET 2005 in where I've written some unit-tests. I'm using Testdriven.NET to execute my unit-tests in Visual Studio, and as long as I executed only one Test-method at a time, everything went fine.
However, when I wanted to run all the test-methods that I've written in that class, Testdriven.NET didn't execute a single one of them. I didn't get any error-message, I just received the message: '0 tests passed, 0 tests failed'.
After some investigating, it turned out that my class didn't had the public
access modifier, and was therefore internal
. Adding the public
access modifier fixed the problem, and all my tests ran smoothly.
After living with this little annoyance for a while, I thought that it should be possible to change this behaviour and make sure that every new class I create in Visual Studio.NET 2005 is marked as public
. In the rare cases that I'll need an internal class, I'll just explicitly change the public
modifier to internal
.
Since every new item that you can create in VS.NET is based on a template (just like a Word document is based on the normal.dot Document Template), it should be rather easy to change this behaviour.
After some digging in the directory-structure of VS.NET 2005, I've finally found where those templates are being kept. They're in this directory:<PF>\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033
(where <PF>
is your Program Files directory, or the directory where you've installed VS.NET)
In this directory, you'll find a number of zip files. I've just unzipped the Class.zip file, and editted the Class.cs file that was in it to this:
using System;
using System.Collections.Generic;
using System.Text;
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}
Then, I've just added the changed template-file back into the zip-file and thought the job was done, so I tried to create a new class in an existing Class Library... Sadly, the new class was still not public by default...
After some searching, it appeared that I had to execute a command which would load the Item Templates into Visual Studio. The command to do so is:devenv /InstallVsTemplates
This finally did the trick! When I add a new class in VS.NET 2005, this new class now has the public access modifier by default!
3 opmerkingen:
This is kinda weird, but i just tried this out without changing anything and for me, the new class was public.
Anyway, whenever i DO meet this problem, i'm glad i now know what to do :)
Are you using VS.NET 2005 ?
When you create a new class-library, the first class is public by default; however, if you create an additional class, the public modifier is not specified (by default).
yes i'm useing 2005 and indeed you are right, the second class I add is not public by default.
Thanks for pointing it out! i just switched to 2005 and this would have given me some pain for sure ^^
Een reactie posten