Instantiate a partic class from an array of a classes that inherit from an interface?

Hopefully this is an easy answer. I’m looking to create a new instance of a Mission class at runtime - I will have multiple mission classes so I want to select the right one and access it via an interface. So i’ll be storing the reference to variable type iMission… the only trouble is I don’t know how to store the array of classes. So I will have Mission_0, Mission_1, and mission_2 classes - all inherting from iMission and although I could easily do:
iMission theMission = new Mission_1();

I need to be able to select the proper class from an Array.
How do I do assign the mission classes to an array of iMission(or whatever) without making new instances of them (as I need to construct them as required):
iMission theMission = new missions1;

Use a generic collection.

public interface iMission {

}

public class Mission : iMission {

}

public class MissionManager {

    List<iMission> missions;

    public void BuildMissionList()
    {
        missions = new List<iMission>();
        missions.Add(new Mission());
    }
}

Seems like that would just be storing references to existing instances of a new class? I’d like to create the classes the same way I would with
iMission theMission = new Mission_1();

only as:

iMission theMission = new x();

or more specifically

iMission theMission = new x1;

Hi there,

Assuming I understand what exactly it is that you’re wanting, here is a simplistic example in C# of how to do it (using a base class instead of an interface - but the basic approach is the same).

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public abstract class classRoot
    {
	public delegate classRoot MyDelegate();
	public static Dictionary<string, MyDelegate> classes = new Dictionary<string,MyDelegate>();
        public static classRoot CreateAClass(string theName)
        {
            foreach (KeyValuePair<string, classRoot.MyDelegate> kvp in classes)
            {
                if (kvp.Key == theName)
                {
                    classRoot myClass = kvp.Value();
                    return myClass;
                }
            }
            
            return null;
        }
        public abstract void DoStuff();
    }

    public class classA : classRoot {
	public static classA CreateMe() { return new classA(); }
        public static void Register() { classRoot.classes.Add("classA", CreateMe); }
	public override void DoStuff() { Console.WriteLine("I got an A!"); }
    }

    public class classB : classRoot {
        public static classB CreateMe() { return new classB(); }
        public static void Register() { classRoot.classes.Add("classB", CreateMe); }
        public override void DoStuff() { Console.WriteLine("I only got a B :("); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            classA.Register();
            classB.Register();

            classRoot myclass = classRoot.CreateAClass("classB");				
	    if ( myclass != null )
            {
                myclass.DoStuff();
            }
	}
    }
}

There are probably better ways to do it (anyone else wan to chime in?), but this should allow you to do what you wanted.

Hope that helps. Please let me know if any of it is unclear.

Will

For the sake of completeness, here’s the same simplistic example but with an interface (as you wanted).

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class classRoot
    {
	public delegate MyInterface MyDelegate();
	public static Dictionary<string, MyDelegate> classes = new Dictionary<string,MyDelegate>();
        public static MyInterface CreateAClass(string theName)
        {
            foreach (KeyValuePair<string, classRoot.MyDelegate> kvp in classes)
            {
                if (kvp.Key == theName)
                {
                    MyInterface myClass = kvp.Value();
                    return myClass;
                }
            }
            
            return null;
        }
    }

    public interface MyInterface
    {
        void DoStuff();
    }

    public class classA : MyInterface {
	public static classA CreateMe() { return new classA(); }
        public static void Register() { classRoot.classes.Add("classA", CreateMe); }
	public void DoStuff() { Console.WriteLine("I got an A!"); }
    }

    public class classB : MyInterface {
        public static classB CreateMe() { return new classB(); }
        public static void Register() { classRoot.classes.Add("classB", CreateMe); }
        public void DoStuff() { Console.WriteLine("I only got a B :("); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            classA.Register();
            classB.Register();

            MyInterface myclass = classRoot.CreateAClass("classB");				
	    if ( myclass != null )
            {
                myclass.DoStuff();
            }
	}
    }
}

It could still be a lot cleaner, but it’ll get the job done. Again, let me know if anything is unclear.

Will