PC Review


Reply
Thread Tools Rate Thread

How can I create an instance of a class (form) with only it's name

 
 
Rene Mansveld
Guest
Posts: n/a
 
      18th Nov 2004
Hi,

how can I create an instance (object) of a class (form) if I only know the
classname (VB.NET 1.0)?
I need to do this in a complex app where jobs consist of parts. Each part's
data is saved in a separate database table. For the parts, the form's class
name is saved in a data table, so that I know which form to use. As this is
a string, I need to be able to create the form by it's name (like
CreateObject() does for COM objects).

--
Any help gladly appreciated!

Rene Mansveld
Spider IT - Germany
www.Spider-IT.de / www.Spider-IT.net

Please reply to the newsgroup(s) )


 
Reply With Quote
 
 
 
 
Imran Koradia
Guest
Posts: n/a
 
      18th Nov 2004
take a look at the Activator.CreateInstance method in the System.Reflection
namespace.

hope that helps..
Imran.

"Rene Mansveld" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi,
>
> how can I create an instance (object) of a class (form) if I only know the
> classname (VB.NET 1.0)?
> I need to do this in a complex app where jobs consist of parts. Each
> part's
> data is saved in a separate database table. For the parts, the form's
> class
> name is saved in a data table, so that I know which form to use. As this
> is
> a string, I need to be able to create the form by it's name (like
> CreateObject() does for COM objects).
>
> --
> Any help gladly appreciated!
>
> Rene Mansveld
> Spider IT - Germany
> www.Spider-IT.de / www.Spider-IT.net
>
> Please reply to the newsgroup(s) )
>
>



 
Reply With Quote
 
Rene Mansveld
Guest
Posts: n/a
 
      18th Nov 2004
Thanks for the quick answer!

Unfortunately I couldn't get this to work, and according to the help, this
will create an instance of a Type.
I tried the Activator.CreateInstance(Nothing, "Auftrag") call, but I got
this exception:
A non handled exception of type 'System.TypeLoadException' occured in
mscorlib.dll.
Extra info: The type Auftrag in the assembly ..., Version=0.3.1783.15723,
Culture=neutral, PublicKeyToken=null could not be loaded.
(translated from german)

Any ideas anyone?

--
Rene Mansveld
Spider IT - Germany
www.Spider-IT.de / www.Spider-IT.net

Please reply to the newsgroup(s) )

"Imran Koradia" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> take a look at the Activator.CreateInstance method in the

System.Reflection
> namespace.
>
> hope that helps..
> Imran.
>
> "Rene Mansveld" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Hi,
> >
> > how can I create an instance (object) of a class (form) if I only know

the
> > classname (VB.NET 1.0)?
> > I need to do this in a complex app where jobs consist of parts. Each
> > part's
> > data is saved in a separate database table. For the parts, the form's
> > class
> > name is saved in a data table, so that I know which form to use. As this
> > is
> > a string, I need to be able to create the form by it's name (like
> > CreateObject() does for COM objects).
> >
> > --
> > Any help gladly appreciated!
> >
> > Rene Mansveld
> > Spider IT - Germany
> > www.Spider-IT.de / www.Spider-IT.net
> >
> > Please reply to the newsgroup(s) )
> >
> >

>
>



 
Reply With Quote
 
Rene Mansveld
Guest
Posts: n/a
 
      18th Nov 2004
It's solved now!

I got an answer from the german newsgroup from Jürgen Luhr which did it.
It is done through Reflexion:

'<deklaration code>
Imports System.Reflexion

'<routine code>
Dim t As Type = Type.GetType("<Namespace>.<Class>")
Dim c As ConstructorInfo = t.GetConstructor(Type.EmptyTypes)
Dim o As Object = c.Invoke(Nothing)
'Now o holds the instance of the class

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany
www.Spider-IT.de / www.Spider-IT.net

Please reply to the newsgroup(s) )

"Rene Mansveld" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Thanks for the quick answer!
>
> Unfortunately I couldn't get this to work, and according to the help, this
> will create an instance of a Type.
> I tried the Activator.CreateInstance(Nothing, "Auftrag") call, but I got
> this exception:
> A non handled exception of type 'System.TypeLoadException' occured in
> mscorlib.dll.
> Extra info: The type Auftrag in the assembly ..., Version=0.3.1783.15723,
> Culture=neutral, PublicKeyToken=null could not be loaded.
> (translated from german)
>
> Any ideas anyone?
>
> --
> Rene Mansveld
> Spider IT - Germany
> www.Spider-IT.de / www.Spider-IT.net
>
> Please reply to the newsgroup(s) )
>
> "Imran Koradia" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > take a look at the Activator.CreateInstance method in the

> System.Reflection
> > namespace.
> >
> > hope that helps..
> > Imran.
> >
> > "Rene Mansveld" <(E-Mail Removed)> wrote in message
> > news:%(E-Mail Removed)...
> > > Hi,
> > >
> > > how can I create an instance (object) of a class (form) if I only know

> the
> > > classname (VB.NET 1.0)?
> > > I need to do this in a complex app where jobs consist of parts. Each
> > > part's
> > > data is saved in a separate database table. For the parts, the form's
> > > class
> > > name is saved in a data table, so that I know which form to use. As

this
> > > is
> > > a string, I need to be able to create the form by it's name (like
> > > CreateObject() does for COM objects).
> > >
> > > --
> > > Any help gladly appreciated!
> > >
> > > Rene Mansveld
> > > Spider IT - Germany
> > > www.Spider-IT.de / www.Spider-IT.net
> > >
> > > Please reply to the newsgroup(s) )
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Rene Mansveld
Guest
Posts: n/a
 
      18th Nov 2004
Correction: Reflexion should be Reflection ;o)

--
Hope this helps ...

Rene Mansveld
Spider IT - Germany
www.Spider-IT.de / www.Spider-IT.net

Please reply to the newsgroup(s) )


"Rene Mansveld" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> It's solved now!
>
> I got an answer from the german newsgroup from Jürgen Luhr which did it.
> It is done through Reflexion:
>
> '<deklaration code>
> Imports System.Reflexion
>
> '<routine code>
> Dim t As Type = Type.GetType("<Namespace>.<Class>")
> Dim c As ConstructorInfo = t.GetConstructor(Type.EmptyTypes)
> Dim o As Object = c.Invoke(Nothing)
> 'Now o holds the instance of the class
>
> --
> Hope this helps ...
>
> Rene Mansveld
> Spider IT - Germany
> www.Spider-IT.de / www.Spider-IT.net
>
> Please reply to the newsgroup(s) )
>
> "Rene Mansveld" <(E-Mail Removed)> schrieb im

Newsbeitrag
> news:(E-Mail Removed)...
> > Thanks for the quick answer!
> >
> > Unfortunately I couldn't get this to work, and according to the help,

this
> > will create an instance of a Type.
> > I tried the Activator.CreateInstance(Nothing, "Auftrag") call, but I got
> > this exception:
> > A non handled exception of type 'System.TypeLoadException' occured in
> > mscorlib.dll.
> > Extra info: The type Auftrag in the assembly ...,

Version=0.3.1783.15723,
> > Culture=neutral, PublicKeyToken=null could not be loaded.
> > (translated from german)
> >
> > Any ideas anyone?
> >
> > --
> > Rene Mansveld
> > Spider IT - Germany
> > www.Spider-IT.de / www.Spider-IT.net
> >
> > Please reply to the newsgroup(s) )
> >
> > "Imran Koradia" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed)...
> > > take a look at the Activator.CreateInstance method in the

> > System.Reflection
> > > namespace.
> > >
> > > hope that helps..
> > > Imran.
> > >
> > > "Rene Mansveld" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > Hi,
> > > >
> > > > how can I create an instance (object) of a class (form) if I only

know
> > the
> > > > classname (VB.NET 1.0)?
> > > > I need to do this in a complex app where jobs consist of parts. Each
> > > > part's
> > > > data is saved in a separate database table. For the parts, the

form's
> > > > class
> > > > name is saved in a data table, so that I know which form to use. As

> this
> > > > is
> > > > a string, I need to be able to create the form by it's name (like
> > > > CreateObject() does for COM objects).
> > > >
> > > > --
> > > > Any help gladly appreciated!
> > > >
> > > > Rene Mansveld
> > > > Spider IT - Germany
> > > > www.Spider-IT.de / www.Spider-IT.net
> > > >
> > > > Please reply to the newsgroup(s) )
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
KSI
Guest
Posts: n/a
 
      18th Nov 2004
Well - its the same with Activator.CreateInstance. You get the type object
from the namespace and class name and pass in the type object to the method:

Dim t As Type = Type.GetType("<Namespace>.<Class>")
Dim frm As Object = Activator.CreateInstance(t)
DirectCast(frm, Form).Show()


Imran.


"Rene Mansveld" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> It's solved now!
>
> I got an answer from the german newsgroup from Jürgen Luhr which did it.
> It is done through Reflexion:
>
> '<deklaration code>
> Imports System.Reflexion
>
> '<routine code>
> Dim t As Type = Type.GetType("<Namespace>.<Class>")
> Dim c As ConstructorInfo = t.GetConstructor(Type.EmptyTypes)
> Dim o As Object = c.Invoke(Nothing)
> 'Now o holds the instance of the class
>
> --
> Hope this helps ...
>
> Rene Mansveld
> Spider IT - Germany
> www.Spider-IT.de / www.Spider-IT.net
>
> Please reply to the newsgroup(s) )
>
> "Rene Mansveld" <(E-Mail Removed)> schrieb im

Newsbeitrag
> news:(E-Mail Removed)...
> > Thanks for the quick answer!
> >
> > Unfortunately I couldn't get this to work, and according to the help,

this
> > will create an instance of a Type.
> > I tried the Activator.CreateInstance(Nothing, "Auftrag") call, but I got
> > this exception:
> > A non handled exception of type 'System.TypeLoadException' occured in
> > mscorlib.dll.
> > Extra info: The type Auftrag in the assembly ...,

Version=0.3.1783.15723,
> > Culture=neutral, PublicKeyToken=null could not be loaded.
> > (translated from german)
> >
> > Any ideas anyone?
> >
> > --
> > Rene Mansveld
> > Spider IT - Germany
> > www.Spider-IT.de / www.Spider-IT.net
> >
> > Please reply to the newsgroup(s) )
> >
> > "Imran Koradia" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed)...
> > > take a look at the Activator.CreateInstance method in the

> > System.Reflection
> > > namespace.
> > >
> > > hope that helps..
> > > Imran.
> > >
> > > "Rene Mansveld" <(E-Mail Removed)> wrote in message
> > > news:%(E-Mail Removed)...
> > > > Hi,
> > > >
> > > > how can I create an instance (object) of a class (form) if I only

know
> > the
> > > > classname (VB.NET 1.0)?
> > > > I need to do this in a complex app where jobs consist of parts. Each
> > > > part's
> > > > data is saved in a separate database table. For the parts, the

form's
> > > > class
> > > > name is saved in a data table, so that I know which form to use. As

> this
> > > > is
> > > > a string, I need to be able to create the form by it's name (like
> > > > CreateObject() does for COM objects).
> > > >
> > > > --
> > > > Any help gladly appreciated!
> > > >
> > > > Rene Mansveld
> > > > Spider IT - Germany
> > > > www.Spider-IT.de / www.Spider-IT.net
> > > >
> > > > Please reply to the newsgroup(s) )
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      18th Nov 2004
"Rene Mansveld" <(E-Mail Removed)> schrieb:
> how can I create an instance (object) of a class (form) if I only know the
> classname (VB.NET 1.0)?


\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamically create class instance from class name Scott Izu Microsoft C# .NET 1 7th Oct 2008 03:20 PM
how to create class instance? André Microsoft VB .NET 4 15th Mar 2007 05:20 PM
How to have New() in a Base Class create and return an instance of a Derived Class? Joe HM Microsoft VB .NET 4 22nd Nov 2005 09:31 PM
How can I create an instance of a class (form) with only it's name Rene Mansveld Microsoft Dot NET Framework Forms 6 18th Nov 2004 05:44 PM
Activator could not create a instance of the class as the Length of the class name is 36 characters Jack Wright Microsoft C# .NET 0 18th Mar 2004 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:37 PM.