473,804 Members | 3,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1860
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.co m

"Mythran" <ki********@hot mail.comwrote in message
news:7B******** *************** ***********@mic rosoft.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********@hot mail.comwrote in message
news:7B******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP02.phx.gbl...
Hi,
"Mythran" <ki********@hot mail.comwrote in message
news:7B******** *************** ***********@mic rosoft.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********@hot mail.comwrote in message
news:Om******** ******@TK2MSFTN GP03.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

4
1740
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final question remaining: How to properly, simple and eleganty implement functions. The functions are encapsulated in the objects, the objects themselves are stored in a binary tree and objects contain 'methods' (functions) along with data. I don't want to...
3
3939
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 want to gaurantee only certain other classes/object types can call a specific method in given class. Any advice would be appreciated and if there is anything that can be done to use the frameworks inherent permissioning. Regards, acg
175
8914
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
12
2817
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown when menu items are clicked.Besides these i would like to put a custom status bar. Any error message encountered in any of the webpage will be displayed in the banner. The problem iam encountering is how to access the customer status bar in child...
6
1777
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic .... CallToStaticMethod();
8
2027
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one of its own, maybe specifying the liquor store over on 44th Street and 5th Avenue or something. Anyway this is what we have so far: base class: "foo" |------------method: "rob_the_liquor_store()" |
4
1187
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 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
9
2093
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
2715
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 class that contains an object of type T) I have a third generic class: MyHandler<Twhich contains 2 Add methods and a generic method: Add(T obj)
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.