我应该重命名按钮事件处理程序名称吗?
当使用 Visual Studio(虽然理想情况下这可以适用于一般情况)并双击我创建的按钮时,自动生成的事件处理程序代码使用以下签名:
Protected Sub btnSubmitRequest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmitRequest.Click
End Sub
最好的做法是保留此方法的名称照原样,或者应该将其重命名为更具描述性的名称,例如 SubmitNewEmployeeRequest?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建另一个名为
SubmitNewEmployeeRequest
的方法。 在btnSubmitRequest_Click
中,调用SubmitNewEmployeeRequest
。 这是最合乎逻辑的职责分离。Create another method called
SubmitNewEmployeeRequest
. InbtnSubmitRequest_Click
, callSubmitNewEmployeeRequest
. That is the most logical separation of duties.此外,如果您在创建处理程序之前更改 IDE 中按钮的名称,处理程序将获得更好的默认名称。 您的按钮的名称当前为 btnSubmitRequest,您可以将其更改为更具体的 btnSubmitNewEmployeeRequest,然后生成处理程序。
您应该为控件命名,并保持控件和处理程序之间的命名一致。
我通常会在使用上下文中命名它们,也就是说,如果您在
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
好吧,我个人保留它,以便我可以快速看到它是一个事件处理程序,特别是单击事件处理程序。 然而,我倾向于只在那里有一行代码来调用(在本例中是您的)SubmitNewEmployeeRequest,因为这也可能从某些上下文菜单中调用,或者响应某些其他事件而触发。
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.