I tried to search for an answer to this but I couldn't find one, the closest I found was: Create an instance of a class from a string but it doesn't really answer my question:
How do I create an instance of a class from a string, e.g. I want to create a WinForms object and add it to MDI.
I have a function that accepts a string, formname (e.g. "Form1"), and checks MDI children for an instance, if it exists it sets focus, if not then it creates an instance and adds it to the children.
The way I currently create a form is with a case statement but I will have to update this every time I add new Forms to the project! Is there a way of adding an instance of a Form class to the MDI children based on the string passed in, e.g. pseudo-code:
call to function: openForm("Form2");
public void openForm(String formname)
{
if form exists in MDI children
{
focus form with name = formname;
}
else
{
MDIChildren.add(CreateInstanceOfClassNamed(formname));
}
}