Click to See Complete Forum and Search --> : Custom inheritance
loupceuxl
July 21st, 2009, 09:23 AM
Hi from what I understand, the built-in System.Windows.Forms.Label class inherits from the System.Windows.Forms.Control class. Is there any way to change the Form.Label class' inheritance to a custom (SectionControl) class which is derived from the Forms.Control class?
so basically,
class SectionControl : Control
class MyLabel : Label
but override/replace the existing implict ‘System.Windows.Forms.Label : Control’ inheritance to ‘System.Windows.Forms.Label : SectionControl’. That way I get the benefits from both the SectionControl and MyLabel specializations. If this is possible I would proceed to add a MyTextBox, MyCheckBox, etc... classes also deriving from the obvious parent and thereby retaining the commonality of all the custom controls in the SectionControl class.
BigEd781
July 21st, 2009, 12:00 PM
You are going about this the wrong way. You don't need to change anything about the Label class, you can just create your own class that inherits from Label and modify it as you see fit. Also, this is just a bad idea in general as you would be changing the framework that people expect to work in a certain (documented) way.
Anyways, you can't do it.
monalin
July 21st, 2009, 12:20 PM
Inheritance in C# only allows you to inherit one class. You will not be able to create a class SectionControl that inherits more than one class. However, I'm not sure if I had a class that did inherit MyLabel, MyTextBox, and MyCheckBox that I would even know what to do with it. If I use this SectionControl anywhere how do I know if it should behave as a label, a textbox, or a checkbox?
About the closest thing you can get is to create one class SectionControl that has each of the mentioned classes as a property of the SectionControl class. For example, you might have something like SectionControl.TextBox or SectionControl.Label. Even with this method, it still doesn't make much sense programing wise.
JonnyPoet
July 21st, 2009, 02:36 PM
What you want to do is done in another way. Obviously you want to interfere the inheritance with an additional section control. This can only make sense if you have some additional methods, properties and events in class section control, which should be common to all of your textboxes, labels... OK simple look at bets practicies for decorator pattern and you will have what you want to get. And thats all about.
vcdebugger
July 22nd, 2009, 12:44 AM
It just like simply creating unnecessary complications trying to forcfully inherit from new user defined class.Sine however label class is derived from Control class in trun you can always override/overload its methods and use its attributes for your functinality which you finally end up there only in turn when you do by using user defined section control class.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.