views:

208

answers:

3

When using Visual Studio (though ideally this can apply to the generic case) and double click on a button I've created, the event handler code that is auto generated uses the following signature:

Protected Sub btnSubmitRequest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmitRequest.Click

End Sub

Is it best practice to leave the name of this method as is, or should it be renamed to something a little more descriptive, such as SubmitNewEmployeeRequest?

+7  A: 

Create another method called SubmitNewEmployeeRequest. In btnSubmitRequest_Click, call SubmitNewEmployeeRequest. That is the most logical separation of duties.

recursive
You could write it as btnSubmit.Click += new delegate { SubmitNewEmployeeRequest(); };
JC
I think that wouldn't work, as it doesn't accept the parameters that an EventHandler requires.
recursive
+1  A: 

Well, personally I leave it so that I can see quickly that it is an event handler, specifically a click event handler. However I would be inclined to just have one line of code there that calls (in this case, your) SubmitNewEmployeeRequest because this may also be called from some context menu as well, or fired in response to some other event.

Tim Jarvis
+2  A: 

Also, if you change the name of the button in the IDE before creating the handlers, the handlers get better default names. The name of your button would currently be btnSubmitRequest, you could change it to be more specific as btnSubmitNewEmployeeRequest and then generate the handler.

You should name your controls, and keep the naming consistent between the control and handlers.
I would generally name them within the context of usage, that is if your in the

  • Employee Request form, then the button need only be named SubmitRequest.
  • Do Stuff to Directory components form, then the button should probably be more descriptive like SubmitNewEmployeeRequest.
Greg Domjan