Events vs Overrides

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

In a form, there is a set of events that can be handled. There is also a
list of (Overrides). So, for the Closed event, for example, there is an
override OnClosed.


Where should exit code be placed: in the Closed event handler, or in the
OnClosed override function?

What is the difference, and why have the two? When would I use each one?


Probably quite basic questions, but it has been puzzling me. The answers to
these questions would also indicate how my own classes should be
constructed.

TIA

Charles
 
Charles,
Where should exit code be placed: in the Closed event handler, or in the
OnClosed override function?
For classes that are derived from Form, I would put the exit code in the
overridden OnClosed method, remembering to call MyBase.OnClosed.

For objects that have a reference to the Form, I would put the exit code in
the Closed event handler code in that object.

In other words the Event is for classes not derived from the base class,
while the OnClosed is for derived classes. OnClosed also allows derived
classes to raise the event, or to supplement what happens when the event is
raised (pre or post processing).

For more details see:

http://msdn.microsoft.com/library/d.../cpgenref/html/cpconEventNamingGuidelines.asp

http://msdn.microsoft.com/library/d...s/cpgenref/html/cpconEventUsageGuidelines.asp

Note VB.NET's RaiseEvent itself will check for handlers attached to the
event, you can simply call RaiseEvent directly, you don't need to check for
Nothing as the above pages show.

Something like:

Public Class Class1

Private WithEvents m_base As Base

Public Sub New()
m_base = New Derived
End Sub

Private Sub Base_Changed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles m_base.Changed
' An aspect of the Base variable changed.
End Sub

End Class

Public Class Base

Public Event Changed As EventHandler

Private m_name As String

Protected Overridable Sub OnChanged(ByVal e As EventArgs)
RaiseEvent Changed(Me, e)
End Sub

Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
OnChanged(EventArgs.Empty)
End Set
End Property

End Class

Public Class Derived
Inherits Base

Private m_value As String

Public Property Value() As String
Get
Return m_value
End Get
Set(ByVal value As String)
m_value = value
OnChanged(EventArgs.Empty)
End Set
End Property

Protected Overrides Sub OnChanged(ByVal e As System.EventArgs)
' The Derived class wants to know if something changed
' In this example both Base & Derived raise this event!

' Do some thing before the event itself is raised
MyBase.OnChanged(e)
' Do some thing after the event itself is raised

End Sub

End Class

Hope this helps
Jay
 
Hi Jay

The kind of response I would have expected from you. A good, solid response.
Thank you.

When calling MyBase.OnClosed, is the recommendation to call it at the start
of the OnClosed method or at the end?

Cheers.

Charles

[I have deleted the rest of the message as I have discovered that Outlook
Express fails to send when I include the remains of previous messages. Any
thoughts?]
 
Charles,
When calling MyBase.OnClosed, is the recommendation to call it at the start
of the OnClosed method or at the end?
I normally put it at the beginning, as it doesn't matter to me if the base
event is raised before or after my code. So I let the event be raised first.

If I want or need my code to execute before the base event is raised, I put
MyBase.OnClosed at the end.

I was trying to show you in the example, that sometimes you may want to put
MyBase.OnClosed in the middle, as you want to do some pre-processing &
post-processing when the base event is raised.

Hope this helps
Jay

Charles Law said:
Hi Jay

The kind of response I would have expected from you. A good, solid response.
Thank you.

When calling MyBase.OnClosed, is the recommendation to call it at the start
of the OnClosed method or at the end?

Cheers.

Charles

[I have deleted the rest of the message as I have discovered that Outlook
Express fails to send when I include the remains of previous messages. Any
thoughts?]
 
Back
Top