Raising Events order

magozeta

New member
Joined
Jun 4, 2012
Messages
2
Programming Experience
1-3
Hello!
I explain what I need :D. I hope I'll be clear.
I have 3 classes in 3 separate dll.
1) Class "BaseForm" that inherits from System.Windows.Forms.Form with "MyEvent" declared as Public Event ()
2) Class "MyForm" which inherits from "BaseForm"
3) class "MyClass" which has a public property "ExternalObject" of "BaseForm" type (the private variable is declared WithEvents)

I make the following steps:
1) intercept the Form LOAD event in "BaseForm"
- Instance the class "MyClass", Assign public property "ExternalObject" = Me
- I do a RaiseEvent "MyEvent" in "BaseForm"
2) "MyEvent" is triggered BEFORE in "MyForm"
3) AFTER "MyEvent" is triggered in "MyClass".
How Can I Raise event Befor in "MyClass" and then in "MyForm"?

thanks
 
The recommended event design pattern is to declare a Protected Overridable On'Event' method that raises the event, and call this method to raise it. Classes that inherit another class should override this method instead of adding event handler, by doing that the inherited class can act before or after it calls the base implementation that raises the event. See for example How to: Implement Events in Your Class
A short example showing inheritance:
    Public Class B
        Public Event Happened As EventHandler

        Protected Overridable Sub OnHappened(e As EventArgs)
            RaiseEvent Happened(Me, e)
        End Sub

        Public Sub CauseHappen()
            Me.OnHappened(EventArgs.Empty)
        End Sub
    End Class


    Public Class A
        Inherits B

        Protected Overrides Sub OnHappened(e As System.EventArgs)
            'before event
            MyBase.OnHappened(e)
            'after event
        End Sub

    End Class

Also notice that B.CauseHappen calls the A.OnHappened overrides when called from an A instance, which in turn calls the base B.OnHappened that raises the event.
 
Back
Top