4
foreach(PlayerBase p in Members)
{
    p.Render();
}

The Members list contains members of type FieldPlayer or GoalKeeper. FieldPlayer and GoalKeeper inherit from PlayerBase. When I call p.Render(), I need the appropriate Render call to be called for FieldPlayer or GoalKeeper. How do I do that?

Thanks

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
pokoko222
  • 101
  • 3

8 Answers8

5

You make the PlayerBase.Render method virtual. See this question for more information.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
3

You're talking about Polymorphism. The proper implementation of Render is automatically called. Make sure the Render method in your PlayerBase class is defined as either abstract or virtual.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
1

If FieldPlayer and GoalKeeper override the Render method, then this will happen automatically.

public abstract class PlayerBase
{
    public virtual void Render();
}

public class FieldPlayer : PlayerBase
{
    public override void Render() {...}
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1

If PlayerBase is an interface

public inteface IPlayerBase 
{
   void Render();
}

public class FieldPlayer : IPlayerBase
{
   public void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : IPlayerBase
{
   public void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

If PlayerBase is an abstract class

public abstract class PlayerBase 
{
   public abstract void Render();
}

public class FieldPlayer : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

If PlayerBase is an class with a virtual function

public class PlayerBase 
{
   public virtual void Render()
   {
      MessageBox.Show("PlayerBase.Render");
   }
}

public class FieldPlayer : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("FieldPlayer.Render");
   }
}

public class GoalKeeper : PlayerBase
{
   public override void Render()
   {
      MessageBox.Show("GoalKeeper.Render");
   }
}

For all three instances, the derived type's Render function will be called.

SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
0

If Render is a virtual member, then the method called will be that on the most derived type of the object at runtime.

mdm
  • 12,480
  • 5
  • 34
  • 53
0

You should declare Render on PlayerBase to be either virtual (if you have a default implementation) or abstract. Then, you override the method in the child classes with the desired behavior.

Jordão
  • 55,340
  • 13
  • 112
  • 144
0

You can mark Render as abstract and then every derived class must implement it.

See this for more information: http://msdn.microsoft.com/en-us/library/ms173152%28v=vs.80%29.aspx

0
abstract class PlayerBase
{
    public abstract void Render();
}

Any class that inherits PlayerBase will be required to implement the Render function. If you have common functionality for Render, you can instead:

abstract class PlayerBase
{
    public virtual void Render() {
        // add common functionality
    }
}

Then in your sub-class, say FieldKeeper:

class FieldKeeper : PlayerBase    {
    public override void Render() {
        // add additional functionality
        base.Render();
    }
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146