473,396 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calling methods or handling events in derived classes.

I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?

Thanks,
Mythran
Mar 19 '07 #1
4 1173
Mythran,

The only case I can see doing this is if you are intending to extend the
class for a specific purpose and what you are doing does not coincide with
the intention of the event. However, if you are deriving from a class, and
what you are doing in the OnXXX override coincides with what you want the
derivations of your class to do, then I don't see why you shouldn't do it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mythran" <ki********@hotmail.comwrote in message
news:7B**********************************@microsof t.com...
>I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?

Thanks,
Mythran


Mar 19 '07 #2
Hi,
"Mythran" <ki********@hotmail.comwrote in message
news:7B**********************************@microsof t.com...
>I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?
I'm not sure of that, IMO there is a reason why the OnXXX are marked as
protected virtual and that's cause it's expected that you have the need to
extend them.

In MSDN you find:
The OnXXX method also allows derived classes to handle the event without
attaching a delegate. This is the preferred technique for handling the event
in a derived class.
Mar 19 '07 #3


"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:e0**************@TK2MSFTNGP02.phx.gbl...
Hi,
"Mythran" <ki********@hotmail.comwrote in message
news:7B**********************************@microsof t.com...
>>I once read an article somewhere describing why we should not override the
OnXXX methods of classes when you want to extend the functionality of a
class. It stated that you should handle the event instead (inside the
derived class). Does anyone know of this article or an article that
discusses this or the opposite (should override OnXXX instead of handling
the event)?

I'm not sure of that, IMO there is a reason why the OnXXX are marked as
protected virtual and that's cause it's expected that you have the need to
extend them.

In MSDN you find:
The OnXXX method also allows derived classes to handle the event without
attaching a delegate. This is the preferred technique for handling the
event in a derived class.
Hmm, I understand this and I have always overrode the OnXXX methods instead
of handling the events. I know I read something somewhere that says you
should not do this (at least in WebControls???) and instead handle the
event....but I can't remember where or the exact instance on when you should
not use the OnXXX methods...if I find it, I'll post it.

Thanks Nicholas and Ignacio,
Mythran

Mar 19 '07 #4
Hi,

"Mythran" <ki********@hotmail.comwrote in message
news:Om**************@TK2MSFTNGP03.phx.gbl...
>
>>

Hmm, I understand this and I have always overrode the OnXXX methods
instead of handling the events. I know I read something somewhere that
says you should not do this (at least in WebControls???) and instead
handle the event....but I can't remember where or the exact instance on
when you should not use the OnXXX methods...if I find it, I'll post it.

Thanks Nicholas and Ignacio,
Mythran
In theory I see no difference between win and web controls. IMO the events
are more intended to the "outside world". as it's the only way you can
inform the external instances of a change in the status of the control
Mar 20 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: acg | last post by:
If you have a class with a public method, and another class which will want to call this method, is there a way to determine the type of the calling class within the method being called? For I...
4
by: Mythran | last post by:
I once read an article somewhere describing why we should not override the OnXXX methods of classes when you want to extend the functionality of a class. It stated that you should handle the event...
9
by: flopbucket | last post by:
Hi, Say I have a baseclass B that has many derived classes, let's say C..Z, for example: class B { }; class C : public B {};
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.