views:

91

answers:

2

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.

+1  A: 

From my understanding, this is a designer issue. Once the code of the page "matures" or before I check-in, I do a search-and-replace of "protected" for "private" then fix any resulting compilation errors.

It is your job, so why would it be "annoying"? :O)

AMissico
That is what I do indeed. But, I like to think of my job as writing code to do more with less.
Carter
+1  A: 

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:

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Yes" />

If I change the visibility of Button1_Click to "private", then I get a yellow screen of death:

Compiler Error Message: CS1061: 'ASP.default_aspx' does not contain a definition for 'Button1_Click' and no extension method 'Button1_Click' accepting a first argument of type 'ASP.default_aspx' could be found (are you missing a using directive or an assembly reference?)

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.

John Saunders
What you describe is the scenario I want to avoid. If my ASPX pages have references to methods in the code-behind, I won't be able to tell if I have broken a link until runtime. Instead, I prefer to keep the methods private and assign event handlers in code rather than declaratively. Keeping the methods private guarantees I don't have to search through the ASPX page for method references to prevent runtime errors.
Carter
@Carter: that means you can't double-click! You can do it manually just fine, but you can't use the designer at all. Even if the designer produced `private` methods, it wouldn't work.
John Saunders
It would work in VB without any special changes because of the "Handles" construct. In C#, there is one extra step where I have to add an event assignment to an initialization function.
Carter
@Carter: in VB, you're saying that the designer doesn't modify the markup? I'm not sure we're talking about the same thing here. I mean, in the Designer, add a new Button, then double-click it. Does the designer in VB.NET not modify the markup to add an OnClick?
John Saunders
@John: VB does touch the ASPX page when you double click in the designer, which is actually a nice feature in VB. It just adds an event handler sub with a "Handles" at the end. I then go in and change "protected" to "private" and I have a nice secure, scoped event handler. In this way you don't get into version conflicts with your page design staff. Try it yourself.
Carter
@Carter: it does, or it does not change the ASPX. If it changes the `Button1` markup to add `OnClick="Button1_Click"`, then adds a `Sub Button1_Click Handles Button1.Click`, then making that `Sub` private won't work.
John Saunders
@John: It does not change ASPX markup if you are using VB. The "Handles" is equivalent to adding an "AddHandler" in an initialization routine. Since private members for all ASPX server controls are generated at compile time everything stays private.
Carter
@Carter: thanks. I'm surprised to hear that the designer actually behaves differently based on the language. If this is the case, then no, you will never be able to do this with C#, since the designer in that case _does_ modify the codebehind, requiring that the method be accessible from codebehind.
John Saunders
@John: If you would like to modify your answer to include the difference in languages and that there is no way to do it I'll accept the answer.
Carter