Home All Groups Group Topic Archive Search About

To create an instance of class in VB2005

Author
31 Oct 2006 2:31 PM
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

Author
2 Nov 2006 1:21 PM
Phill W.
Jordi Julià wrote:
Show quoteHide quote
> 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.
Author
3 Nov 2006 2:31 PM
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!




Show quoteHide quote
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> escribió en el mensaje
news:eicrc3$5jh$1@south.jnrs.ja.net...
> Jordi Julià wrote:
>> 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.