To create an instance of class in VB2005

J

Jordi Julià

Hello,

I need to create with VB2005 an instance of one of the classes of the
project in run time.

For example:

- The Namespace root and the name of the project are he himself: "Project"
- The project has including three class: Class_1.vb, Class_2.vb and
Class_3.vb

In VB2003 it did of the following way:

Dim NameClass as string ="Class_1"
Dim Ensam as Type = Type.GetType("Project" & Chr(46) & NameClass)
Dim result as Object = Activator.CreateInstance(Ensam)


As it would have to do it in VB2005?
if I execute the Type Ensam is Nothing

Thanks!
Jordi
 
P

Phill W.

Jordi said:
Hello,

I need to create with VB2005 an instance of one of the classes of the
project in run time.

For example:

- The Namespace root and the name of the project are he himself: "Project"
- The project has including three class: Class_1.vb, Class_2.vb and
Class_3.vb

In VB2003 it did of the following way:

Dim NameClass as string ="Class_1"
Dim Ensam as Type = Type.GetType("Project" & Chr(46) & NameClass)
Dim result as Object = Activator.CreateInstance(Ensam)


As it would have to do it in VB2005?
if I execute the Type Ensam is Nothing

The above works for me - are you sure your classes haven't got
themselves nested inside some other class?

Try creating an instance of one directly and see what Type it thinks it is:

Dim c2 as New Class_2

Debug.Writeline( c2.GetType().ToString() )

HTH,
Phill W.
 
J

Jordi Julià

Hello,

I have proven what you have said to me and:

Dim c2 As New Class_2 '(Class_2 exist in
Project)
dim Name as string = c2.GetType().ToString 'Name="Project.c2"
Dim ensam As Type = Type.GetType(Name) 'ensam = Nothing


Because 'ensam' Nothing has left?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top