I have a program that needs to navigate through a series of screens in a given order. What I’d like to do is to manage this centrally, using something analogous to a class factory, where I send a request for the next form, and it instantiates and returns the next form. I have the following, however, this will instantiate all the forms immediately:
private List<Form> screens = new List<Form>() { new Form1(), new Form2(), … };
private Form currentForm;
private int currentPos;
public Form Next()
{
currentForm = screens[++currentPos];
return currentForm;
}
Is there a way to defer instantiation until the actual request is made? For example:
private List<Form> screens = new List<Form>() { Form1,Form2, …};
private Form currentFrm;
private int currentPos;
public Form Next()
{
currentForm = new screens[++currentPos];
return currentFrm;
}
(this won’t compile)
One way to do that is to store types in your list and use Activator.CreateInstance() to dynamically create the form instances: