CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2009
    Posts
    1

    Custom inheritance

    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.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Custom inheritance

    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.

  3. #3
    Join Date
    Jul 2006
    Posts
    297

    Re: Custom inheritance

    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.

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Custom inheritance

    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.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Custom inheritance

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured