为什么子进程不能同时实现接口和处理事件?

发布于 2024-10-31 23:32:50 字数 197 浏览 2 评论 0原文

为什么子进程不能同时实现接口和处理事件?

以下给出了我的语法错误:

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

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

发布评论

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

评论(2

凉城已无爱 2024-11-07 23:32:50

该语法错误与语言语法一致,见VB语言规范第9.2.1节1

子声明 ::=
[ 属性 ] [ procedureModifier+ ] SubSignature [ HandlesOrImplements ] LineTerminator

End Sub 语句终止符

HandlesOrImplements ::= HandlesClause | ImplementsClause

因此任何一种方法只支持一种。该规范并未(快速查看)包含此限制的基本原理。为此,您需要与 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:

SubDeclaration ::=
[ Attributes ] [ ProcedureModifier+ ] SubSignature [ HandlesOrImplements ] LineTerminator
Block
End Sub StatementTerminator

and

HandlesOrImplements ::= HandlesClause | ImplementsClause

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.

单挑你×的.吻 2024-11-07 23:32:50

我从未见过关于 VB.NET 团队为何做出此决定的任何详细讨论,但说实话,我很难从 OOP 设计的角度看出这有何意义。事件处理程序方法通常不应该执行任何工作。相反,他们应该调用其他方法来完成繁重的工作。他们调用的另一种方法是实现您的接口的方法。

但如果您执行以下操作,这当然是可以实现的:

Public MustInherit Class MyParentClass
    Protected WithEvents MyButton As Button

    Protected MustOverride Sub MySub() Handles MyButton.Click
End Class

Public Class MyDerivedClass : Inherits MyParentClass : Implements IMyInterface
    Protected Overrides Sub MySub() Implements IMyInterface.MyMethod
        ' Do something here...
    End Sub
End Class

还请记住,事件处理程序方法通常具有独特的签名;类似于:

Public Sub MySub(ByVal sender As System.Object, ByVal e As System.EventArgs)

这是事件处理程序方法能够实现接口中定义的方法的另一个原因。

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:

Public MustInherit Class MyParentClass
    Protected WithEvents MyButton As Button

    Protected MustOverride Sub MySub() Handles MyButton.Click
End Class

Public Class MyDerivedClass : Inherits MyParentClass : Implements IMyInterface
    Protected Overrides Sub MySub() Implements IMyInterface.MyMethod
        ' Do something here...
    End Sub
End Class

Also remember that event handler methods generally have a distinctive signature; something like:

Public Sub MySub(ByVal sender As System.Object, ByVal e As System.EventArgs)

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.

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