Mustoverride Event?

  • Thread starter Thread starter Dave Wurtz
  • Start date Start date
D

Dave Wurtz

All,

Is there a way to force a derived class to contain an event. I have done
this with methods (i.e. Protected MustOverride Sub Test), but can I do
something similar with an event (Protected MustOverride Event MyEvent
doesn't work for obvious reasons)?

Thanks in advance.
Dave
 
Dave,

I don't think it is possible to force a derived class to override an event.
You can of course make them override a protected method that is used in the
base class to raise the event. E.g.

-------------------
*Base Class*

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub

-------------------

When you want to raise the event, simply call RaiseNameChanged. This way,
you know that the derived class must handle OnNameChanged before the event
is raised.

Hope this helps,

Trev.
 
Codemonkey,
Protected Mustoverride Sub OnNameChanged(e as EventArgs)

Not the best name for the MustOverride method, as OnNameChanged is normally
for the method to raise the event, per the Event Usage Guidelines in the
Design Guidelines for CLass Library Developers.
Private Sub RaiseNameChanged()
me.OnNameChanged(EventArgs.Empty)
raiseevent NameChanged(me, EventArgs.Empty)
End Sub
Making RaiseNameChanged private, how are derived classes suppose to call it?
Or did I miss read you?

Don't get me wrong, I see where you are heading with this, and I like the
idea!

Hope this helps
Jay
 
Dave,
In addition to what Codemonkey stated:
Is there a way to force a derived class to contain an event.
A derived class will always contain (expose) the event, unfortunately there
is no real way to force a derived class to raise an event. What we normally
do is give derived classes the opportunity to raise the base classes events,
then take it on faith they will use it appropriately.

The Event Usage Guidelines in the Design Guidelines for Class Library
Developers, has defined a pattern that allows derived classes to raise an
event of a base class.

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

Public Class Base

Public Event NameChanged(sender as Object, e as EventArgs)

Protected Overrideable Sub OnNameChanged(e as EventArgs)
RaiseEvent NameChanged(me, e)
End Sub

Public Property Name() As String
...
Set(ByVal value As String)
...
OnNameChanged(EventArgs.Empty)
...
End Set
End Property

End Class

When ever the base class or derived classes need to raise the NameChanged
event they call the OnNameChanged method. Derived classes can override this
method to replace or supplement what happens when the NameChanged event
occurs. Which means if the derived classes still want the event to be raised
they need to use MyBase.OnNameChanged in their overridden OnNameChanged.

Which is similar to what Codemonkey showed. If the above does not address
your needs then a variation of what Codemonkey may be what you need.

Hope this helps
Jay
 
Cheers for pointing out the giudelines. Guess I should have put a bit of
extra thought into the naming.
Making RaiseNameChanged private, how are derived classes suppose to call it?
Or did I miss read you?

From the origional post, I took it that Dave wanted to force derived classes
to *handle* the event. I didn't think he wanted to raise them from there. I
tend to default to "less is more" when defining my methods (by making them
private unless I explicitly need to have them exposed), but obviously this
isn't the best option in all cases - especially when following the
guidelines on events.

Thanks again,

Trev.
 
Just out of interest, what would be the best way of making the derived class
override the method that raises the event, while still guaranteeing the
event is raised (if you use MustOverride, then you can't add code to the
method body to raise the event).

I dunno why you would ever want to do this, but just thought I'd ask anyway
(too much time on my hands ;)

Trev.
 
Codemonkey,
From the origional post, I took it that Dave wanted to force derived classes
to *handle* the event. I didn't think he wanted to raise them from there.
I
I'm not sure what Dave wanted. Hopefully between your & my post he provides
a little more info on what he is wanting.

Jay

Codemonkey said:
Cheers for pointing out the giudelines. Guess I should have put a bit of
extra thought into the naming.


From the origional post, I took it that Dave wanted to force derived classes
to *handle* the event. I didn't think he wanted to raise them from there. I
tend to default to "less is more" when defining my methods (by making them
private unless I explicitly need to have them exposed), but obviously this
isn't the best option in all cases - especially when following the
guidelines on events.

Thanks again,

Trev.
<<snip>>
 
Trev,
Unfortunately I don't really think you can. As the class that defines the
event has to raise the event. There is no code that you can put in a base
class per se that tells a derived class that it has to execute it.

The closest I can come up with is basically your example, which is not fool
proof ;-). The only real change I was proposing to your sample was to make
the names more consistent with the Guidelines.

Jay

Codemonkey said:
Just out of interest, what would be the best way of making the derived class
override the method that raises the event, while still guaranteeing the
event is raised (if you use MustOverride, then you can't add code to the
method body to raise the event).

I dunno why you would ever want to do this, but just thought I'd ask anyway
(too much time on my hands ;)

Trev.
<<snip>>
 
Unfortunately I don't really think you can.

I don't see why you would want to anyway. Best keeping things simple in my
opinion.
 
Back
Top