By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

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

P: n/a
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) :o)
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
take a look at the Activator.CreateInstance method in the System.Reflection
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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) :o)

Nov 21 '05 #2

P: n/a
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) :o)

"Imran Koradia" <no****@microsoft.com> schrieb im Newsbeitrag
news:uH**************@TK2MSFTNGP12.phx.gbl...
take a look at the Activator.CreateInstance method in the System.Reflection namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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) :o)


Nov 21 '05 #3

P: n/a
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) :o)

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> schrieb im Newsbeitrag
news:Op**************@TK2MSFTNGP12.phx.gbl...
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) :o)

"Imran Koradia" <no****@microsoft.com> schrieb im Newsbeitrag
news:uH**************@TK2MSFTNGP12.phx.gbl...
take a look at the Activator.CreateInstance method in the

System.Reflection
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
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) :o)



Nov 21 '05 #4

P: n/a
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) :o)
"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> schrieb im Newsbeitrag
news:uC*************@TK2MSFTNGP11.phx.gbl...
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) :o)

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> schrieb im Newsbeitrag news:Op**************@TK2MSFTNGP12.phx.gbl...
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) :o)

"Imran Koradia" <no****@microsoft.com> schrieb im Newsbeitrag
news:uH**************@TK2MSFTNGP12.phx.gbl...
take a look at the Activator.CreateInstance method in the

System.Reflection
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
> 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) :o)
>
>



Nov 21 '05 #5

P: n/a
KSI
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" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:uC*************@TK2MSFTNGP11.phx.gbl...
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) :o)

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> schrieb im Newsbeitrag news:Op**************@TK2MSFTNGP12.phx.gbl...
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) :o)

"Imran Koradia" <no****@microsoft.com> schrieb im Newsbeitrag
news:uH**************@TK2MSFTNGP12.phx.gbl...
take a look at the Activator.CreateInstance method in the

System.Reflection
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
> 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) :o)
>
>



Nov 21 '05 #6

P: n/a
"Rene Mansveld" <R.********@TAKETHISOUT.Spider-IT.de> 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/>

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.