Turn off auto-add of subroutines in Visual Studio
My project is built in VB.Net
Many times I find that Visual Studio has added subroutines to my code files even if a subroutine of the exact same name already exists. This can cause debugging nightmares as the new empty routine seems to override the correct routine. I think this can happen if I double-click on a control in the form Design view, but I try not to do this.
Is there any way to turn this off?
Example:
Hand entered
Private Sub TS_Main_View_Network_Click Handles TS_Main_View_Network.Click
System added:
Private Sub TS_Main_View_Network_Click( ByVa开发者_运维知识库l sender As System.Object, ByVal e As System.EventArgs) Handles TS_Main_View_Network.Click
I guess that the system adds the second routine because the argument list (which is unneeded but may be required) list is missing from the first routine.
In the example you gave in the comments the methods are overloaded.
I assume you added the first one by hand because the signature is not correct for an event handler. .NET event handlers should have a signature that matches void EventHandler(object sender, EventArgs e).
Private Sub TS_Main_View_Network_Click Handles TS_Main_View_Network.Click
Private Sub TS_Main_View_Network_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TS_Main_View_Network.Click
When Visual Studio adds an event handler on double click (which you can't disable out-of-the-box) it always creates a method with the correct signature.
Actually, your code takes advantage of some VB features (late-binding, perhaps) to let your declare an event handler without arguments. You cannot do that in a purely static-typed language, like C#, because it is not valid IL: the method without arguments cannot be assigned to an EventHandler
delegate (the type of the Click
event). Behind the scenes the VB compiler creates some sort of adapter for the method with no arguments.
This happens when you write a handler for a control that does not exist, then add the control to the form in design mode, and then double click on that control.
精彩评论