locked
Need Help with Inheritance

    Question

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

    class GraphicsDeviceControl : Control
    {
    }

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

    class TriangleControl : GraphicsDeviceControl
    {
             protected override void Draw()
             {
            
             }
    }

    I want to overrride the Draw() method without creating a class that inherits from the Control class. Basically I want to be able to change what Draw() does and then run it - but I dont want to make another Control class.

    How can I do this? Your help is much appreciated.

    Thank you

    Q
    Sunday, January 17, 2010 12:12 AM

All replies

  • I'm not sure I follow.

    You need to inherit from the class whose implementation of Draw() you want to override.  That is simply what override means.

    There may be other solutions to your underlying problem, if you could explain in much more detail where exactly things are going wrong for you.
    Sunday, January 17, 2010 12:39 AM
  • Hi Binary Code. Thanks for replying to my post. I am trying to run a modified Draw() method using code - but because Draw() is inside a class that inherits from the Control class - I cant do this without placing a control on the form. Do you understand?

    So my basic need is to be able to run a modified Draw() method without having to place a control on the form.
    Sunday, January 17, 2010 1:11 AM
  • Consider having your Draw method raise an event.  Then the form could implement a handler for this event and customize the behavior.  Thus, you would only have to place the control on the Form once.  This assumes that you are okay with placing one instance of the Control on the Form.

    If you do not want to place a Control on the Form, there probably is little reason to inherit from Control.

    Sunday, January 17, 2010 1:38 AM
  • Hi again Binary Code. Thanks - I think thats exactly what I wanted. I will mark your post as the answer after Ive implemented what u said if thats ok with you.
    Sunday, January 17, 2010 12:45 PM
  • Hi Binary Code. Ive read up on events but I am confused as to how this would implement what I want.
    Sunday, January 17, 2010 1:13 PM
  • Hi,

    As I learnt from your post, you are trying the modify one method of a class .
    I'm afraid it's not possible.

    Could you please be more specific why do not want to inherit from the class and override the method?
    It seems this is the only way we can call our method.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 25, 2010 3:08 AM
  • It is apparent that you have not told the whole story, so I will speculate on the rest.  You wish to do this....

            static void ControlTest()
            {
                Control c = new Control();
                Control gdc = new TriangleControl();  // A GraphicsDeviceControl object.
                c.Draw();
                gdc.Draw();
            }

    ...so that you can take advantage of polymorphism.  However, you are running into an obstacle.  You do not own the Control class, the Draw method is not marked "virtual", and the compiler is reporting an error that you have yet to mention.  Something to the effect that "you cannot override inherited member because it is not marked virtual, abstract or override".  So you are forced to do this....


        class TriangleControl : GraphicsDeviceControl
        {
            public new void Draw()
            {
            }
        }


    ...use the "new" keyword so that it can compile.  But, now you lose the polymorphism. The inheritance chain is broken, which is forcing you to realize that you just might need to write your own Control class with a method, Draw, declared as virtual. 

    The solution is to do it this way.....


        class Control
        {
            public void Draw()
            {
            }
        }
        class GraphicsDeviceControl : Control
        {
            public new virtual void Draw()
            {
                base.Draw();
            }
        }
        class TriangleControl : GraphicsDeviceControl
        {
            public override void Draw()
            {
            }
        }

    Use these classes like this....


        class Program
        {
            static void Main(string[] args)
            {
                ControlTest();
                Console.ReadLine();
            }
            static void ControlTest()
            {
                Control c = new TriangleControl();
                GraphicsDeviceControl gdc = new TriangleControl();
                c.Draw();  // calls Control.Draw
                gdc.Draw();  // calls TraingleControl.Draw
            }
        }


    All of this is related to questions and issues that you have with the XNA Framework, too, isn't it?

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."
    • Edited by Rudedog2MVP Tuesday, January 26, 2010 9:06 PM spelling
    Monday, January 25, 2010 8:52 PM