472,143 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Variable type Object, assign to a Class at runtime

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

Nov 20 '05 #1
4 4167
"Andres" <an*************@catv.rol.ch> schrieb
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.
--
Armin

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


Its a class in the system namespace

System.Activator

http://msdn.microsoft.com/library/de...ClassTopic.asp

Hope this helps
Jay

"Andres" <an*************@catv.rol.ch> wrote in message
news:0e****************************@phx.gbl...
-----Original Message-----
"Andres" <an*************@catv.rol.ch> schrieb
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.


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.

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.


Where can I find this Activator and what is it?

Andres
--
Armin

.

Nov 20 '05 #3
"Andres" <an*************@catv.rol.ch> schrieb
-----Original Message-----
"Andres" <an*************@catv.rol.ch> schrieb
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.


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.
I am not sure if it helps, but have a look at

Activator.Createinstance.


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. :-)
--
Armin

Nov 20 '05 #4
Hello,

"Andres" <an*************@catv.rol.ch> schrieb:
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?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

83 posts views Thread by Alexander Zatvornitskiy | last post: by
5 posts views Thread by Patrick | last post: by
134 posts views Thread by James A. Donald | last post: by
166 posts views Thread by Graham | last post: by
3 posts views Thread by mra | last post: by

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.