opinions with OnXXX overrides.

P

Peter Rilling

I would like people's opinions. Many classes have an Onxxx that actually
invokes the registered event handlers. A derived class can override these
methods rather then registering an event. The consequence of this is that
the overridden class must call the parents' method so that events are
dispatched.

My question is, is it better to call the parents' method at the beginning of
the override, or at the end, as the last statement?
 
T

Tim Wilson

I guess it all depends on what the method is suppose to do and what you are
trying to accomplish. Look at OnPaint, for example. Would you like the
end-developer, or yourself, to be able to draw on the surface of the control
through the Paint event? If you call the base classes OnPaint first then any
painting that you do may paint over anything that a registered Paint event
handler has done. If you call OnPaint last then you are allowing a
registered Paint event handler to draw over any painting that you did. So I
guess it all depends on what you are trying to accomplish and what the base
class is actually doing.
 
S

Samuel R. Neff

Depends on the event what the base class is doing besides raising the
event, if anything.

If the event is cancellable then and you want your processing to only
be done if nothing cancelled the event, then call base first. If you
want the first crack at cancelling the event, then call base last.

In other cases there could be additional processing in the base that
you want to do either before or after your code and it depends
entirely on what specific processing is done. There is no general
rule.

HTH,

Sam


I would like people's opinions. Many classes have an Onxxx that actually
invokes the registered event handlers. A derived class can override these
methods rather then registering an event. The consequence of this is that
the overridden class must call the parents' method so that events are
dispatched.

My question is, is it better to call the parents' method at the beginning of
the override, or at the end, as the last statement?

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top