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
Is there any difference between the above and using Handles MyBase.MyEvent, as per the guidance here under the heading Handling Events Inherited From a Base Class?
With this simple example, there's little difference. You could argue that by using Handles MyBase... that it's clearer for future readers of the code to know where the event comes from.
One potential reason for preferring one or the other is in the (unusual) situation of creating an event with the same name in your derived class - the difference between Me and MyBase allows you to be specific as to which event you're handling:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With