AddHandler 与 Handles - 有什么区别?

发布于 2024-08-18 09:18:59 字数 171 浏览 5 评论 0原文

了解Handles只是在构造函数中添加AddHandler的一种方法 code>,但是一般来说这两个是等价的吗?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

℡寂寞咖啡 2024-08-25 09:18:59

附加事件处理程序的确切时间及其周围发生的情况存在一些差异。例如,当使用 WithEventsHandles 时,编译器将发出代码,包装对保存在属性中公开事件的实例的变量的访问,并在属性设置器内它将把事件处理程序与前一个实例(如果有)分离,然后将事件处理程序附加到新实例(如果有)。

这意味着,如果您采用以下代码示例,对 mm 的访问将表现不同:

' WithEvents approach '
Dim WithEvents mm As SomeClass
Sub Main()
    mm = New SomeClass()
    Dim nn As SomeClass = mm
End Sub

' Other approach '
Dim mm As SomeClass
Sub Main()
    mm = New SomeClass()
    Dim nn As SomeClass = mm
End Sub

WithEvents 情况下,mm = New SomeClass() 实际上会调用一个属性 setter,而 Dim nn As SomeClass = mm 将从属性 getter 中获取值,而在第二种情况下,不会为该值创建任何属性,但是代码将直接访问该字段。

There is some difference in exactly when the event handler is attached, and what is going on around it. For instance, when using WithEvents and Handles, the compiler will emit code that wraps access to the variable holding the instance that exposes the event in a property, and inside the property setter it will detach the event handler from the previous instance (if any), and then attach the event handler to the new instance (if any).

This means that if you take the following code samples, the access to mm will behave differently:

' WithEvents approach '
Dim WithEvents mm As SomeClass
Sub Main()
    mm = New SomeClass()
    Dim nn As SomeClass = mm
End Sub

' Other approach '
Dim mm As SomeClass
Sub Main()
    mm = New SomeClass()
    Dim nn As SomeClass = mm
End Sub

In the WithEvents case, mm = New SomeClass() will in fact call a property setter, and Dim nn As SomeClass = mm will fetch the value from a property getter, while in the second case, there will be no property created for the value, but the code will access the field directly.

亣腦蒛氧 2024-08-25 09:18:59
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs)

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'this

    AddHandler Button2.Click, AddressOf Button2_Click

    'now Button2_Click looks like this

    'Private Sub Button2_Click(ByVal sender As System.Object, _
    'ByVal e As System.EventArgs) Handles Button2.Click

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs)

End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'this

    AddHandler Button2.Click, AddressOf Button2_Click

    'now Button2_Click looks like this

    'Private Sub Button2_Click(ByVal sender As System.Object, _
    'ByVal e As System.EventArgs) Handles Button2.Click

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