Events and Inheritance Question 
Author Message
 Events and Inheritance Question

Suppose a Parent Class declares an event handler to handle some event
and attaches it with the += operator.  Can the parent declare that method as
virtual? so that a Child class can override it so that the Child's method
will
receive the notification instead of the parent?  Then the child can pass the
message
up to the parent if it wants to?

Thanks,

james



Mon, 13 Dec 2004 23:59:29 GMT  
 Events and Inheritance Question
James,

    Yes you can.  The delegate will call the virtual method, which, if
overriden in a child class, will have the child's implementation called.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Suppose a Parent Class declares an event handler to handle some event
> and attaches it with the += operator.  Can the parent declare that method
as
> virtual? so that a Child class can override it so that the Child's method
> will
> receive the notification instead of the parent?  Then the child can pass
the
> message
> up to the parent if it wants to?

> Thanks,

> james



Tue, 14 Dec 2004 00:07:11 GMT  
 Events and Inheritance Question
Classes that raise events typically have an overridable method to raise the
event.  For instance you can trap a textbox's keypress event, but if you
derive from textbox, you can override its OnKeyPress method, giving you the
first shot at the the keypress.  Remember though that you need to then call
the base class event so that it can raise the event.


Quote:
> Suppose a Parent Class declares an event handler to handle some event
> and attaches it with the += operator.  Can the parent declare that method
as
> virtual? so that a Child class can override it so that the Child's method
> will
> receive the notification instead of the parent?  Then the child can pass
the
> message
> up to the parent if it wants to?

> Thanks,

> james



Tue, 14 Dec 2004 19:41:49 GMT  
 Events and Inheritance Question
Robert,

That is exactly what I want, thanks.

james


Quote:
> Classes that raise events typically have an overridable method to raise
the
> event.  For instance you can trap a textbox's keypress event, but if you
> derive from textbox, you can override its OnKeyPress method, giving you
the
> first shot at the the keypress.  Remember though that you need to then
call
> the base class event so that it can raise the event.



> > Suppose a Parent Class declares an event handler to handle some event
> > and attaches it with the += operator.  Can the parent declare that
method
> as
> > virtual? so that a Child class can override it so that the Child's
method
> > will
> > receive the notification instead of the parent?  Then the child can pass
> the
> > message
> > up to the parent if it wants to?

> > Thanks,

> > james



Tue, 14 Dec 2004 22:30:55 GMT  
 Events and Inheritance Question
James,

Have you also seen the topic in MSDN's Design Guidelines for Class Library
Developers?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpge...
ml/cpconeventusageguidelines.asp

To deal with the problem of remembering to call the base class method have a
look at this article which elaborates on a technique I discovered from Tomas
Restrepo.

http://www.codeproject.com/dotnet/eventstrategy.asp

Kevin


Quote:
> Robert,

> That is exactly what I want, thanks.

> james



> > Classes that raise events typically have an overridable method to raise
> the
> > event.  For instance you can trap a textbox's keypress event, but if you
> > derive from textbox, you can override its OnKeyPress method, giving you
> the
> > first shot at the the keypress.  Remember though that you need to then
> call
> > the base class event so that it can raise the event.



> > > Suppose a Parent Class declares an event handler to handle some event
> > > and attaches it with the += operator.  Can the parent declare that
> method
> > as
> > > virtual? so that a Child class can override it so that the Child's
> method
> > > will
> > > receive the notification instead of the parent?  Then the child can
pass
> > the
> > > message
> > > up to the parent if it wants to?

> > > Thanks,

> > > james



Wed, 15 Dec 2004 00:53:18 GMT  
 Events and Inheritance Question
Kevin,

Very interesting.  Thanks for the link

james


Quote:
> James,

> Have you also seen the topic in MSDN's Design Guidelines for Class Library
> Developers?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpge...
Quote:
> ml/cpconeventusageguidelines.asp

> To deal with the problem of remembering to call the base class method have
a
> look at this article which elaborates on a technique I discovered from
Tomas
> Restrepo.

> http://www.codeproject.com/dotnet/eventstrategy.asp

> Kevin



> > Robert,

> > That is exactly what I want, thanks.

> > james



> > > Classes that raise events typically have an overridable method to
raise
> > the
> > > event.  For instance you can trap a textbox's keypress event, but if
you
> > > derive from textbox, you can override its OnKeyPress method, giving
you
> > the
> > > first shot at the the keypress.  Remember though that you need to then
> > call
> > > the base class event so that it can raise the event.



> > > > Suppose a Parent Class declares an event handler to handle some
event
> > > > and attaches it with the += operator.  Can the parent declare that
> > method
> > > as
> > > > virtual? so that a Child class can override it so that the Child's
> > method
> > > > will
> > > > receive the notification instead of the parent?  Then the child can
> pass
> > > the
> > > > message
> > > > up to the parent if it wants to?

> > > > Thanks,

> > > > james



Fri, 24 Dec 2004 22:36:46 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. WinForm Event Inheritance Question

2. Delegates, Events, Inheritance

3. Events and Inheritance

4. Visual Inheritance and Events

5. single inheritance and function inheritance

6. Inheritance typeid question

7. Inheritance Question

8. Inheritance question

9. Basic Inheritance (??) question

10. some question about Inheritance

11. Questions about inheritance and implementing abstract classes

12. Inheritance question...

 

 
Powered by phpBB® Forum Software