Krell
Krell

Reputation: 55

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class(possibly by casting it back to the Derived class since you know it was originally a Derived class)

public class MySystem
{
    public string name;

    MySystem(string name)
    {
        this.name = name;
    }

    public void Update()
    {
        //dostuff
    }
}

public class PowerSystem : MySystem
{
    public int totalPower;

    PowerSystem (string name, int power) : base(name)
    {
        this.totalPower = power;
    }

    public void Update()
    {
        base.Update();
        //Do other stuff
    }
}

void Main()
{
    List<MySystem> SystemList = new List<MySystem>();

    SystemList.Add(new System("Shields"));
    SystemList.Add(new System("Hull"));

    Power p = new Power("Power", 10);
    SystemList.Add(p);

    foreach(MainSystems ms in SystemList)   
    {
        if(ms.name != "Power")
            ms.Update();
        else
            (PowerSystem)ms.Update();   //This doesn't work
    }

}

So what I'm trying to do is run the update method for every element in the list, with the exeption of the one I named power and instead run the Power.Update method.

The closest post I have found to answering this is here unfortunately I don't fully understand it.

I'm hoping that the list is holding a reference to PowerSystem p and that somehow I can convert it and access the PowerSystem menthod.

I hope this is clear. Thanks

PS if you have a better idea for this I'm all ears.

Upvotes: 2

Views: 95

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

Use polymorphism - mark Update in base class virtual and override it in derived class.

Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. At run-time, when client code calls the method, the CLR looks up the run-time type of the object, and invokes that override of the virtual method. Thus in your source code you can call a method on a base class, and cause a derived class's version of the method to be executed.

public class MySystem
{
    public string name;

    MySystem(string name)
    {
        this.name = name;
    }

    public virtual void Update()
    {
        //dostuff
    }
}

public class PowerSystem : MySystem
{
    public int totalPower;

    PowerSystem (string name, int power) : base(name)
    {
        this.totalPower = power;
    }

    public override void Update()
    {
        base.Update();
        //Do other stuff
    }
}

Now, PowerSystem.Update() will get called automatically

foreach(MainSystems ms in SystemList)   
{
    ms.Update();
}

For MySystem instances it will call MySystem.Update, but for PowerSystem instances the override will be called.

Upvotes: 3

Related Questions