0

When one extends a class, how is the proper way of setting code to be executed upon some event of the base class?

Should one handle the base class event this way:

Public Class Class2
    Inherits Class1

    Private Sub MyBase_MyEvent(sender as object, e as eventargs) Handles MyBase.MyEvent
        ' do stuff here
    End Sub
End Class

Or should one override the event caller sub?

Public Class Class2
    Inherits Class1

    Protected Overrides Sub OnMyEvent(e as EventArgs)
        MyBase.OnMyEvent(e)
        ' do stuff here
    End Sub
End Class

Thank you very much

  • I believe this would be better for StackOverflow, as it's a problem tightly tied to a specific language. – Andy Mar 8 '17 at 20:52
  • I see. Do you think I should delete the post? – VBobCat Mar 8 '17 at 20:53
  • 1
    It's just my personal input at the moment, perhaps someone else will have a different opinion. You can leave it here for now, I suppose. – Andy Mar 8 '17 at 20:56
  • 1
    @DavidPacker when referring other sites, it is often helpful to point that cross-posting is frowned upon – gnat Mar 10 '17 at 7:49
1

Both options are fine and common. The big difference: with option 1, you can't necessarily tell what order the handlers are fired in, while with the second option, you can decide whether MyBase.OnMyEvent is called before or after your overridden implementation.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.