generic Form frmX = new based on class name

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application where I say essentially:

Type typeForm = Type.GetType(stringPassedIn)
Form frmX = null;
if (typeForm == Type.GetType("somenamespace.someform") {
frmX = new somenamespace.someform()
} else if (typeForm = Type.GetType("somenamespace.anotherform") {
frmX = new somenamespace.anotherform()
} else ....// and so an ad naseum for all the forms

Every time we add a form, we have to add it to this code. I want something much more generic.

What I want to be able to do is something like this

Type typeForm = Type.GetType(stringPassedIn)
Form frmX = new //something that uses the typeForm or stringPassedIN

Regards,

Madman Pierre aka Peter Horwood
 
madmanpierre said:
I have an application where I say essentially:

Type typeForm = Type.GetType(stringPassedIn)
Form frmX = null;
if (typeForm == Type.GetType("somenamespace.someform") {
frmX = new somenamespace.someform()
} else if (typeForm = Type.GetType("somenamespace.anotherform") {
frmX = new somenamespace.anotherform()
} else ....// and so an ad naseum for all the forms

Every time we add a form, we have to add it to this code. I want
something much more generic.

If you're going to have to do specific things for different types, you
might as well avoid using GetType so often and do straight string
comparisons:

Form frmX = null;
switch (stringPassedIn)
{
case "somenamespace.someform":
frmX = new somenamespace.someform();
break;

// etc
}
What I want to be able to do is something like this

Type typeForm = Type.GetType(stringPassedIn)
Form frmX = new //something that uses the typeForm or stringPassedIN

Use Activator.CreateInstance.
 
Back
Top