While developing and ASP.NET application in C# or VB using Visual Studio 2005/2008/2010 (Not a problem in 2003), if I create a new method automatically by double-clicking on a control in the designer or picking the new method in the code editor dropdowns (VB only), the access modifier is always protected instead of private. This is annoying because my developers have to manually change the method to private every time.
Is there are way to tell visual studio to generate all new method headers as private instead of protected?
Please do not debate the reasons for wanting my methods to be private.
This can’t work for the case where you double-click a control in the designer.
Double clicking a control in the designer not only creates in code-behind the code for the handler for the default event, it also changes the markup to refer to it. For instance, having added a Button to a web page, then double-clicking on it, I get:
If I change the visibility of
Button1_Clickto “private”, then I get a yellow screen of death:The ASP.NET page is parsed and built to generate a class that derives from your codebehind class. That class needs to be able to reference things like your event handler.
Apparently, when used with VB.NET, this problem does not exist. The difference is that in VB.NET, the designer does not change the markup at all, so the class generated from the markup does not need to refer to the created event handler. It can safely be made Private.
However, since the designer, when used with C# does modify the markup such that the generated class does need to refer to the new event handler. In this case, the event handler cannot be made private.