views:

34

answers:

2

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( ByVal 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.

A: 

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.

xpda
+1  A: 

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.

Martinho Fernandes
Why do I need ( ByVal sender As System.Object, ByVal e As System.EventArgs)I am not using the event arguements?Everything seems to work without the argument list and it certainly is easier to read
NormD
For your use case you may not need the arguments, but some times you do need them. I'll clarify this in the answer.
Martinho Fernandes
Of course, I keep the arguments when I need them, but for most of my cases I don't need them.
NormD
You can't disable that behavior out of the box. It might be that an extension could do that, but I am not aware of anything like that.
Martinho Fernandes