473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
6 2387
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
6
by: SamIAm | last post by:
Hi am creating a email application that needs to mail out a very large amount of emails. I have created a multithreaded c# application that using message queuing. I have created a threadpool of 5...
2
by: Bill D | last post by:
In a simple Windows forms application, the main form is called Form1. Within this form's class, I refer to the the running instance of this main class as "this" as in this.Text = "NEW TEXT" I...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
3
by: Clinton Pierce | last post by:
I can create a delegate like this, and everything works fine: class Foo { private delegate void NextPanel(); private NextPanel myself; // And later in a method private void EffStart() {
6
by: wu jianhua | last post by:
hi. If I have a form, like FrmAbout, can I create a form instance only with a string "FrmAbout"? not like : Form frm = new FrmAbout(); I want the code : Form frm = createInstance( "FrmAbout" );...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.