TechTalkz.com LogoAsk the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Activator.CreateInstance problem

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 11-12-2007, 12:24 PM   #1
Soulless
Guest
 
Posts: n/a
Activator.CreateInstance problem

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(asForm Name));
loForm.MdiParent = this;
loForm.Show();
}


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

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

I get: value cannot be null.

Any why I am getting null?

Thanks!
  Reply With Quote
Old 11-12-2007, 12:25 PM   #2
Mattias Sjögren
Guest
 
Posts: n/a
Re: Activator.CreateInstance problem

>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
|
Please reply only to the newsgroup.
  Reply With Quote
Old 11-12-2007, 12:26 PM   #3
Soulless
Guest
 
Posts: n/a
Re: Activator.CreateInstance problem

On Dec 10, 3:45 pm, Mattias Sjögren <>
wrote:
> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|
> Please reply only to the newsgroup.


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.
  Reply With Quote
Old 11-12-2007, 12:26 PM   #4
Soulless
Guest
 
Posts: n/a
Re: Activator.CreateInstance problem

On Dec 10, 3:45 pm, Mattias Sjögren <>
wrote:
> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|
> Please reply only to the newsgroup.


Thanks, yes, I needed to affix my namespace info before the form name.
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site?Need Help?

All times are GMT +5.5. The time now is 08:38 AM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional