我应该重命名按钮事件处理程序名称吗?

发布于 2024-07-11 03:14:44 字数 297 浏览 6 评论 0原文

当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满栀 2024-07-18 03:14:44

创建另一个名为 SubmitNewEmployeeRequest 的方法。 在btnSubmitRequest_Click中,调用SubmitNewEmployeeRequest。 这是最合乎逻辑的职责分离。

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

顾北清歌寒 2024-07-18 03:14:44

此外,如果您在创建处理程序之前更改 IDE 中按钮的名称,处理程序将获得更好的默认名称。 您的按钮的名称当前为 btnSubmitRequest,您可以将其更改为更具体的 btnSubmitNewEmployeeRequest,然后生成处理程序。

您应该为控件命名,并保持控件和处理程序之间的命名一致。
我通常会在使用上下文中命名它们,也就是说,如果您在

  • 员工请求表单中,则该按钮只需命名为 SubmitRequest。
  • Do Stuff to Directory 组件表单,那么该按钮可能应该更具描述性,如 SubmitNewEmployeeRequest。

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.
入怼 2024-07-18 03:14:44

好吧,我个人保留它,以便我可以快速看到它是一个事件处理程序,特别是单击事件处理程序。 然而,我倾向于只在那里有一行代码来调用(在本例中是您的)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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文