473,986 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2444
take a look at the Activator.Creat eInstance method in the System.Reflecti on
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.Creat eInstance(Nothi ng, "Auftrag") call, but I got
this exception:
A non handled exception of type 'System.TypeLoa dException' occured in
mscorlib.dll.
Extra info: The type Auftrag in the assembly ..., Version=0.3.178 3.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****@microso ft.com> schrieb im Newsbeitrag
news:uH******** ******@TK2MSFTN GP12.phx.gbl...
take a look at the Activator.Creat eInstance method in the System.Reflecti on namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.Reflexio n

'<routine code>
Dim t As Type = Type.GetType("< Namespace>.<Cla ss>")
Dim c As ConstructorInfo = t.GetConstructo r(Type.EmptyTyp es)
Dim o As Object = c.Invoke(Nothin g)
'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.********@TAK ETHISOUT.Spider-IT.de> schrieb im Newsbeitrag
news:Op******** ******@TK2MSFTN GP12.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.Creat eInstance(Nothi ng, "Auftrag") call, but I got
this exception:
A non handled exception of type 'System.TypeLoa dException' occured in
mscorlib.dll.
Extra info: The type Auftrag in the assembly ..., Version=0.3.178 3.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****@microso ft.com> schrieb im Newsbeitrag
news:uH******** ******@TK2MSFTN GP12.phx.gbl...
take a look at the Activator.Creat eInstance method in the

System.Reflecti on
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.********@TAK ETHISOUT.Spider-IT.de> schrieb im Newsbeitrag
news:uC******** *****@TK2MSFTNG P11.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.Reflexio n

'<routine code>
Dim t As Type = Type.GetType("< Namespace>.<Cla ss>")
Dim c As ConstructorInfo = t.GetConstructo r(Type.EmptyTyp es)
Dim o As Object = c.Invoke(Nothin g)
'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.********@TAK ETHISOUT.Spider-IT.de> schrieb im Newsbeitrag news:Op******** ******@TK2MSFTN GP12.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.Creat eInstance(Nothi ng, "Auftrag") call, but I got
this exception:
A non handled exception of type 'System.TypeLoa dException' occured in
mscorlib.dll.
Extra info: The type Auftrag in the assembly ..., Version=0.3.178 3.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****@microso ft.com> schrieb im Newsbeitrag
news:uH******** ******@TK2MSFTN GP12.phx.gbl...
take a look at the Activator.Creat eInstance method in the

System.Reflecti on
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.Creat eInstance. 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>.<Cla ss>")
Dim frm As Object = Activator.Creat eInstance(t)
DirectCast(frm, Form).Show()
Imran.
"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:uC******** *****@TK2MSFTNG P11.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.Reflexio n

'<routine code>
Dim t As Type = Type.GetType("< Namespace>.<Cla ss>")
Dim c As ConstructorInfo = t.GetConstructo r(Type.EmptyTyp es)
Dim o As Object = c.Invoke(Nothin g)
'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.********@TAK ETHISOUT.Spider-IT.de> schrieb im Newsbeitrag news:Op******** ******@TK2MSFTN GP12.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.Creat eInstance(Nothi ng, "Auftrag") call, but I got
this exception:
A non handled exception of type 'System.TypeLoa dException' occured in
mscorlib.dll.
Extra info: The type Auftrag in the assembly ..., Version=0.3.178 3.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****@microso ft.com> schrieb im Newsbeitrag
news:uH******** ******@TK2MSFTN GP12.phx.gbl...
take a look at the Activator.Creat eInstance method in the

System.Reflecti on
namespace.

hope that helps..
Imran.

"Rene Mansveld" <R.********@TAK ETHISOUT.Spider-IT.de> wrote in message
news:%2******** *******@TK2MSFT NGP11.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.********@TAK ETHISOUT.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 CreateClassByNa me( _
ByVal PartialAssembly Name As String, _
ByVal QualifiedClassN ame As String _
) As Object
Return _
Activator.Creat eInstance( _
[Assembly].LoadWithPartia lName( _
PartialAssembly Name _
).GetType(Quali fiedClassName) _
)
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
3657
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 puposes only and is not normally visable to the user.) The Thread function is actually in the Form class. Now.. What I am seeing is that when I create an instance of this Class Library's Form, which starts the worker thread, it seems to hose up...
6
3350
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 threads and each thread checks the queue, receives the message and sends an email. How do I determine the right amount of threads to create? Thanks, S
2
12072
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 want to do something like change this Text on the Form1 window from within another class. Trying Form1.Text = "TEXT FROM CLASS" or Form1.ActiveForm.Text = "TEXT FROM CLASS"
0
3658
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 datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
3
3944
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
5584
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" ); How to code my createInstance function? Thanks.
27
3838
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 updates the same data in all other four tables in the right places. I know it would be possible by using the ForeignKeyConstraint object. I have created the tables using the DataSet Visual Tool and I know it doesn't create any ForeignKeyConstraint obj....
4
12473
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
6625
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 object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream. XML serialization serializes only the public fields and property values of an object...
0
10392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11475
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11664
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10137
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5217
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.