C# Inheritance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • quddusaliquddus
    New Member
    • May 2009
    • 23

    C# Inheritance

    Hi :),
    I have two classes. The first one inherits from the Control class...

    Code:
    class GraphicsDeviceControl : Control
    {
    }

    ... and one that inehrits from that one ...

    Code:
    class TriangleControl : GraphicsDeviceControl
    {
             protected override void Draw()
             {
            
             }
    }
    Both of these classes inherit from the Control class.

    I want to overrride the Draw() method.

    But if I inherit from the TriangleControl class, then the new class also turns into a control. This is because the TriangleControl class inherits from the Control class.

    How can I overrride the Draw() method without the class doing the overrriding turning into a a control?

    Your help is much appreciated.

    Thank you

    Q
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    How can I overrride the Draw() method without the class doing the overrriding turning into a control?
    You don't. That is the nature of inheritance.

    But the real questions is *why* do you think you need to? What do you perceive to the be the problem with it being a control? Just about everything on screen is actually inherited from 'control': Window.Form ... Label ...

    If it helps, you can cast it to a triangle any time you need to.

    Code:
    ((TriangleControl)sender).SomeMethodInsideAtriangle();
    You could add an enum inside the GrahicsDeviceCo ntrol that you set upon creation of a new inherited control to hold its type/purpose

    Code:
    public enum GraphicShape
    {
       UNKNOWN,
       RETANGLE,
       OVAL,
       TRIANGLE,
       POLYGON,
    }
    When you create the object set a value

    Code:
    GraphicShape myShape;
    // Constructor
    public TriangleShape()
    {
       myShape = GraphicShape.Triangle; // Down you have a variable you can easily check
    }
    But I'm pretty sure you could just use the TypeOf method to find out what the type is of your class.

    So I guess that circles around to me asking "Why do you think you have a problem with it being a 'control' ?"

    Comment

    • quddusaliquddus
      New Member
      • May 2009
      • 23

      #3
      Hi. Thanks for the help. I really appreciate it.

      My problem with it inheriting the control class is that I dont know how I can overrride the Draw() method from anyhwere else in the application.

      For example - if I wanted to run a different version of the Draw() method form a button click - how could I do this?

      Thanks again.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You wouldn't. How the control draws is how you design it.
        You don't have a way to make new Draw() methods for any other control from outside of those controls, right? You can't just change how a textbox draws based on a button click.

        If you have some known ways that you want it to draw, then you could build those methods into the control, and use a parameter to tell the control how to draw itself. The ProgressBar control is a good example. You can set it's style to 'blocks', 'continuous' or 'marque'

        For your shapes you may just need to add parameters such as 'color', 'line weight', 'shadow' and so on. Then you can make a triangle, red, weight 5, shadow = true and so on.

        This is a deliberate design concept. This way the new control will have a consistent appearance and you don't have code in one class tightly bound to and affecting another class. You don't have huge amounts of duplicate code all over. You define how it is to be drawn only in one place. If you need to give it a new 'style' you do that in the control, then you can make use of it everyplace elese. If you have to FIX how it is being drawn you still only have to do it in one place, and not chase down all the places you copy/pasted some override into a dozen different forms and applications.

        Comment

        • quddusaliquddus
          New Member
          • May 2009
          • 23

          #5
          Thank you for the help

          Comment

          Working...