Variable type Object, assign to a Class at runtime

  • Thread starter Thread starter Andres
  • Start date Start date
A

Andres

Hi all,

I have the problem to assign a variable of type object to
a specific class at runtime. I want to use a string
variable that specify the class a want to set this object.

Is something similar to add a new control to a form using
the controls collection:

Private Sub CommandCreateInstance_Click()
Dim newInstance As Object
Dim strClass as String

strClass = "aClass"

Set newInstance = Me.Controls.Add("VB." & strClass,
newInstanceName, Me)

End Sub

In the example 'aClass' is a Class I made as a Class
module, but with custom classes doesn't work, it has to be
a Control registered in VB.

Has anybody any idea to do this.

Thanks
 
Andres said:
Hi all,

I have the problem to assign a variable of type object to
a specific class at runtime.

You can not assign an object to a class. You can assign an object to a
variable with a certain type.
I want to use a string
variable that specify the class a want to set this object.

The string contains the class name? Where, when and how do you create the
object?
Is something similar to add a new control to a form using
the controls collection:

Private Sub CommandCreateInstance_Click()
Dim newInstance As Object
Dim strClass as String

strClass = "aClass"

Set newInstance = Me.Controls.Add("VB." & strClass,
newInstanceName, Me)

End Sub

In the example 'aClass' is a Class I made as a Class
module, but with custom classes doesn't work, it has to be
a Control registered in VB.

Has anybody any idea to do this.


I am not sure if it helps, but have a look at Activator.Createinstance.
 
Andres,
Where can I find this Activator and what is it?

Its a class in the system namespace

System.Activator

http://msdn.microsoft.com/library/d...cpref/html/frlrfSystemActivatorClassTopic.asp

Hope this helps
Jay

Andres said:
-----Original Message-----


You can not assign an object to a class. You can assign an object to a
variable with a certain type.

Ok, may be I didn't describe very well what I try to
do... I declare a variable as Object type

Dim newInstance as Object

then at runtime I want newInstance to point to an object
of class 'x' that the user can choose from a set of
classes types displayed in a combobox or whatever. So when
the user clicks the button insert new object the procedure
should receive a class name as a string variable, then
should create the object of class 'x' and should store the
reference in a collection of objects somewhere.

object.

The string contains the class name? Where, when and how do you create the
object?



I am not sure if it helps, but have a look at Activator.Createinstance.

Where can I find this Activator and what is it?

Andres
 
Andres said:
Ok, may be I didn't describe very well what I try to
do... I declare a variable as Object type

Dim newInstance as Object

then at runtime I want newInstance to point to an object
of class 'x' that the user can choose from a set of
classes types displayed in a combobox or whatever. So when
the user clicks the button insert new object the procedure
should receive a class name as a string variable, then
should create the object of class 'x' and should store the
reference in a collection of objects somewhere.

The mentioned activator class should solve this problem.
Where can I find this Activator and what is it?

where: in the object browser, by using the symbol search or in the docs.
what it is: what you are looking for. :-)
 
Hello,

Andres said:
I have the problem to assign a variable of type object to
a specific class at runtime. I want to use a string
variable that specify the class a want to set this object.

Are you sure you are talking about VB.NET?
 
Back
Top