XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Inheritance question, about virtual and pure virutalness in C#.

Last post 11/21/2009 2:02 PM by test84. 3 replies.
  • 11/20/2009 10:30 AM

    Inheritance question, about virtual and pure virutalness in C#.

    Hi,

    I collect a reference to all my enemies in a List that is made from their base type, MovableObject. Whenever I want to draw them, I get an iterator with the type of MovableObject and iterate over my List to draw each of them. Obviously the draw() method in MovableObject is empty and each enemy type has it's own Draw method.

    I have no problem iterating over enemies, since each of them is an MovableObject, the problem lies when I want to call their respective Draw method call. In another words, how can I tell an enemy that is casted to an MovableObject that when I call draw upon you, you have to call your own Draw call, don't call your base's draw call.

    Since my understanding of inheritance is not solid, I have some feeling that it lies with pure virtualness and something like it.

    Thanks.

    EDIT
    I do know that I have to cast my enemis to movable object, I just want their draw method to be called upon their respective draw call, not their base on.
    I remember a dynamic cast on C++ can do something similar, if not exactly the same.
    Try not. Do, or do not. There is no try.
  • 11/20/2009 11:44 AM In reply to

    Re: Inheritance question, about virtual and pure virutalness in C#.

    Answer
    Reply Quote
    Just define your Draw method on the MoveableObject class as virtual then use the override keyword on the subclass, then the sublcass Draw method will be called.

    There are no pure virtual methods in c# - the abstract keyword is used instead. You can use the abstract keyword if you want instead of virtual. The difference is abstract methods don't have a method body, and the (first concrete) subclass is forced to implement one.

    class MoveableObject
    {
         public virtual void Draw() {}
    }

    or

    class MoveableObject
    {
         public abstract void Draw();
    }

    class Enemy : MoveableObject
    {
         public override void Draw() {} // this one will be called
    }
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 11/20/2009 3:22 PM In reply to

    Re: Inheritance question, about virtual and pure virutalness in C#.

    Answer
    Reply Quote
    Craig Martin:
    class MoveableObject
    {
    public abstract void Draw();
    }
    You'd actually have to do this:

    abstract class MoveableObject
    {
    public abstract void Draw();
    }

    A non-abstract class cannot have abstract methods. 
  • 11/21/2009 2:02 PM In reply to

    Re: Inheritance question, about virtual and pure virutalness in C#.

    Thanks, Since both of you gave me the answer, I didn't know which I should've pressed as the answer without insulting the other person.

    The problem is solved for draw method, and since I have this problem with Update() too, I'll read more about Inheritance and then ask more questions.

    Thanks again!
    Try not. Do, or do not. There is no try.
Page 1 of 1 (4 items) Previous Next