none
Visual Studio 2015 - first event handler stops event? Or "Handles" doesn't work properly?

    Question

  • I'm working on a project which has currently migrated to Visual Studio 2015. I've just encountered a problem I so far have no idea what to do with. In a chain of inherited classes, looking like this:

    Public Class ControlA
        Public Event CustomEvent(sender As Object, e As CustomEventArgs)
    End Class
    
    Public Class BaseForm
        Public WithEvents C As ControlA
    End Class
    
    Public Class MiddleForm
        Inherits BaseForm
    
        Private Sub C_CustomEvent(sender As Object, e As CustomEventArgs) Handles C.CustomEvent
            'do something
        End Sub
    End Class
    
    Public Class FinalForm
        Inherits MiddleForm
    
        Private Sub C_CustomEvent(sender As Object, e As CustomEventArgs) Handles C.CustomEvent
            'do something elese
        End Sub
    End Class

    the event handler in the most "outer" class (in this case - a form that is shown to a user) do what it's supposed to do and, I think, stops the event. Handlers in the base class are not being called.

    Everything worked fine in VS2010. Moreover - everything works fine when I use AddHandler at least in MiddleForm. If both subs are defined with "Handles" - it breaks as described above.

    Any piece of advice how to solve this? I'd be ery grateful.

    Friday, June 24, 2016 9:47 AM

Answers

  • After some research I've found the reason.

    Turns out, it's not the matter of a handler magically stopping event's chain, but the compiler in VS2015 (Roslyn?) is generating different code than it used to. It's possible to see it using MSIL. The compiler auto-generates a property for C in FinalForm, but its setter has a huge bug - it uses BaseForm.C instead of MiddleForm.C. That causes MiddleForm's handler to just be omitted.

    Similar bug is already reported at Roslyn github.

    So far there is one solution - handle the event in base class using a Protected Overridable handler and overwrite it in derived classes.


    Friday, June 24, 2016 1:30 PM