Activator.CreateInstance problem

  • Thread starter Thread starter Soulless
  • Start date Start date
S

Soulless

I have added a method to my main form that is generic and will open a
window given the window name as a string. Unfortunatley, it fails...

Here is the method:

private void OpenWindow(string asFormName)
{
foreach (Form f in this.MdiChildren)
{
if (asFormName == f.GetType().ToString())
{
f.Activate();
return;
}
}

Form loForm =
(Form)Activator.CreateInstance(Type.GetType(asFormName));
loForm.MdiParent = this;
loForm.Show();
}


I call the method: OpenWindow("fornname");

This line fails: Form loForm =
(Form)Activator.CreateInstance(Type.GetType(asFormName));

I get: value cannot be null.

Any why I am getting null?

Thanks!
 
Any why I am getting null?

Type.GetType returns null if it can't find the type ("fornname" in
this case). Maybe you've misspelled the type name, or you're missing
the namespace.


Mattias
 
Type.GetType returns null if it can't find the type ("fornname" in
this case). Maybe you've misspelled the type name, or you're missing
the namespace.

Mattias

It's odd. I verified that the form name is correct. I had not
considered it a namespace issue but will take a look. Thanks.
 
Type.GetType returns null if it can't find the type ("fornname" in
this case). Maybe you've misspelled the type name, or you're missing
the namespace.

Mattias

Thanks, yes, I needed to affix my namespace info before the form name.
 
Back
Top