为什么子进程不能同时实现接口和处理事件?
为什么子进程不能同时实现接口和处理事件?
以下给出了我的语法错误:
Sub MySub() Handles MyButton.Click Implements MyInterface.MyMethod
End Sub
我知道我可以用另一种方法处理这种逻辑,但这不是重点。我只是想了解这背后的原因。
Why can't a sub implement an interface and handle event at the same time?
The following gives my a syntax error:
Sub MySub() Handles MyButton.Click Implements MyInterface.MyMethod
End Sub
I know I could handle this kind of logic with another method, but that is not the point. I just want to understand the reasoning behind this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该语法错误与语言语法一致,见VB语言规范第9.2.1节1:
和
因此任何一种方法只支持一种。该规范并未(快速查看)包含此限制的基本原理。为此,您需要与 Microsoft VB 语言设计团队的人员交谈。
1 这包含在 VS 安装中,位于 <VSRoot›\VB\Specifications\1033 下。
The syntax error is consistent with the language grammar, in §9.2.1 of the VB language specification1:
and
So only one is supported on any one method. The specification doesn't (on a quick view) include a rationale for this limitation. For that you'll need to talk to someone on the VB language design team at Microsoft.
1 This is included with an installation of VS under ‹VSRoot›\VB\Specifications\1033.
我从未见过关于 VB.NET 团队为何做出此决定的任何详细讨论,但说实话,我很难从 OOP 设计的角度看出这有何意义。事件处理程序方法通常不应该执行任何工作。相反,他们应该调用其他方法来完成繁重的工作。他们调用的另一种方法是实现您的接口的方法。
但如果您执行以下操作,这当然是可以实现的:
还请记住,事件处理程序方法通常具有独特的签名;类似于:
这是事件处理程序方法也能够实现接口中定义的方法的另一个原因。
I've never seen any detailed discussion of why this decision was made by the VB.NET team, but to be honest, I struggle to see how this makes any sense from an OOP design perspective. Event handler methods generally shouldn't be doing any work. Rather, they should call out to other methods to do the heavy lifting. The other method that they call out to would be the one that implements your interface.
But it's certainly attainable if you do something like this:
Also remember that event handler methods generally have a distinctive signature; something like:
which is another reason why it would be extremely uncommon for an event handler method to also be able to implement a method defined in an interface.