TankorSmash
TankorSmash

Reputation: 12767

Using Child methods from a List<Parent> objects

Currently I've got a ton of methods that are all similar: From a list of Parents, I'm casting an object to it's proper type and then Drawing it. This works fine, but is extremely unwieldy as each method is exactly identical other than the cast.

It looks something like this

public class Parent
{ 
    public virtual void Draw()
    {
        //deliberately nothing
    }
}

public class Child1 : Parent
{ 
    public override void Draw()
    {
        //draw this object, but slightly different method than Parent
    }
}

public class Child2 : Parent
{ 
    public override void Draw()
    {
        //draw this, but slightly different method than Child1 and Parent
    }
}

/////////////////////////

List<Parent> parent_list = new List<Parent>();
parent_list.Add(new Child1());
parent_list.Add(new Child2());

/////////////////////////

foreach (Parent parent in parent_list)
{
    parent.Draw(); //Would like to use child1 and child2's draw
}

/////////////////////////

///instead I'm doing a manual cast for each child class
foreach (Parent parent in parent_list)
{
    Child1 child = (Child1)parent;
    child.Draw();
}

foreach (Parent parent in parent_list)
{
    Child2 child = (Child2)parent;
    child.Draw();
}

The issue I'm running into is that it's trying to call Parent.Draw() when I want to it to call Child.Draw() I'm positive there's a better way to design the code, but I can't figure it out.

How can I call elegantly call Draw on all the elements of in list when the only thing in common is their parent?

Upvotes: 0

Views: 1234

Answers (2)

TankorSmash
TankorSmash

Reputation: 12767

Lazrberezosky's answer was correct for the general case, so I've marked his a correct.

But for my personal issue, it was that one of the many Parent classes I was using was marked as virtual instead of override so it incorrectly looking above the Parent class into its parent, which may have well been Object and giving me an error.

 Object.Draw    
 Parent.Draw - Virtual    
  Child.Draw - Override //incorrectly was virtual in my code    
G_Child.Draw - Override

When I called Parent.Draw on the list, it went to Parent.Draw and saw that Child.Draw was virtual so then, I think, it went back up to Object.Draw and threw a compiler error.

Feel free to edit for clarity.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

I suppose your child classes are inherited from Parent (otherwise it wouldn't be possible to add child object's to parents collection and have Draw method overridden). Also I don't understand why you are calling to this.Draw inside Draw methods? It will cause recursive calls. You should have method implementations there

public class Parent 
{ 
    public virtual void Draw()
    {
       // parent implementation of Draw
    }
}

public class Child1 : Parent
{ 
    public override void Draw()
    {
        // child1 implementation of Draw
    }
}

public class Child2 : Parent
{ 
    public override void Draw()
    {
        // use base.Draw() to call parent implementation
        // child2 implementation of Draw
    }
}

Then when you do

foreach (Parent parent in parent_list)
{
    parent.Draw(); 
}

Overridden (child) methods will be called here due to polymorphism.

Upvotes: 2

Related Questions