Object name as variable

  • Thread starter Thread starter Sorin Sandu
  • Start date Start date
S

Sorin Sandu

How can I declare a new object with a variable name ?
I'm trying to add many objects with s for each loop
for ....
Object varname = new Object() ......
 
How can I declare a new object with a variable name ?
I'm trying to add many objects with s for each loop
for ....
Object varname = new Object() ......

if i understand you correctly you want pass the name of the object type in
a string...

using System.Reflection;

Assembly.GetExecutingAssembly().CreateInstance(.....)

or

Activator.CreateInstance(....)
 
Sorin Sandu said:
How can I declare a new object with a variable name ?

You can't.
I'm trying to add many objects with s for each loop
for ....
Object varname = new Object() ......

That sounds like a perfect place to use an array, or an ArrayList.
 
How can I declare a new object with a variable name ?
I'm trying to add many objects with s for each loop
for ....
Object varname = new Object() ......

You don't need a new name for each object. If you store the objects in an
array they will each get a "name" as ArrayList[x].

If you need to add your own name, one way could be to create a new struct
having a name and an object.
 
That would be interesting code if u could :D
Imagine debugging that :D

How can I declare a new object with a variable name ?
I'm trying to add many objects with s for each loop
for ....
Object varname = new Object() ......

You don't need a new name for each object. If you store the objects in an
array they will each get a "name" as ArrayList[x].

If you need to add your own name, one way could be to create a new struct
having a name and an object.
 
My point is you only need a reference to the object, not the original name
of it.
 
Back
Top