Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handles Me.Event vs Handles MyBase.Event

Tags:

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

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?

like image 386
Zev Spitz Avatar asked Nov 08 '25 15:11

Zev Spitz


1 Answers

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
like image 186
James Thorpe Avatar answered Nov 10 '25 12:11

James Thorpe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!