处理Me.Event vs Handles MyBase.Event

时间:2015-09-21 12:55:57

标签: vb.net events

Class BaseClass
    Public Event MyEvent(sender As Object, e As EventArgs)
End Class
Class DerivedClass
    Inherits BaseClass
    Sub DerivedClassEventHandler(sender As Object, e As EventArgs) Handles Me.MyEvent
        'Do something here
    End Sub
End Class

根据处理从基类继承的事件标题下的guidance here,上述内容与使用Handles MyBase.MyEvent之间是否存在差异?

1 个答案:

答案 0 :(得分:1)

通过这个简单的例子,没有什么区别。您可以通过使用Handles MyBase...来证明代码的未来读者更清楚地了解事件的来源。

选择其中一个或另一个的一个潜在原因是在(不寻常)情况下在派生类中创建具有相同名称的事件 - MeMyBase之间的差异允许您成为具体到您正在处理的事件:

Class BaseClass
    Public Event MyEvent(sender As Object, e As EventArgs)
End Class
Class DerivedClass
    Inherits BaseClass

    Public Shadows Event MyEvent(sender As Object, e As SomeOtherEventArgs)

    Sub EventHandler(sender As Object, e As EventArgs) Handles MyBase.MyEvent

    End Sub

    Sub EventHandler(sender As Object, e As SomeOtherEventArgs) Handles Me.MyEvent

    End Sub
End Class