Create objetc dynamically

  • Thread starter Thread starter Bruno Remeyse
  • Start date Start date
B

Bruno Remeyse

Hi,

I need help for the following problem :
At the start of my application, i found the items of the configuration in
the register. This items represent tools connected to the system at this
moment.
I have a class named "Machine_Tools" for drive the tools by an RS232 series
communication.
These items are stored like string so i have a string table to catch them.
exemple : items are like "AX001", "AX002", etc ... and they have spécifics
caractéristics for acces to.(stored in subskeys).

So I need to create objects whose the name is "AX001" or "AX006", or .... to
be able to drive them
How can i do to make something like :
"Machine_Tools <Variable> = new Machine_Tools(); " where <Variable> content
the name of the Machine_Tool object , so that i could make "AX001.start()"
or other public method of this object in my program ?
How can i do to execute the inside of a variable ?

Thank a lot for your help ...

Bruno
 
Variable names are only useful for the compiler. When the code compilers,
the symbols are transmitted to generic references and therefore any variable
that might be named AX001 would not make any sense at runtime. What you
might need is maybe a Hashtable where you can associate a key, in this case
a text string, with an object. For instance,

Hashtable hash = new Hashtable();
hash.Add("AX001", new Machine_Tool());
hash.Add("AX002", new Machine_Tool());

((Machine_Tool)hash["AX001"]).start();
 
It's a very good solution.
Thank a lot for your help.

Bruno


Peter Rilling said:
Variable names are only useful for the compiler. When the code compilers,
the symbols are transmitted to generic references and therefore any
variable
that might be named AX001 would not make any sense at runtime. What you
might need is maybe a Hashtable where you can associate a key, in this
case
a text string, with an object. For instance,

Hashtable hash = new Hashtable();
hash.Add("AX001", new Machine_Tool());
hash.Add("AX002", new Machine_Tool());

((Machine_Tool)hash["AX001"]).start();


Bruno Remeyse said:
Hi,

I need help for the following problem :
At the start of my application, i found the items of the configuration in
the register. This items represent tools connected to the system at this
moment.
I have a class named "Machine_Tools" for drive the tools by an RS232 series
communication.
These items are stored like string so i have a string table to catch
them.
exemple : items are like "AX001", "AX002", etc ... and they have
spécifics
caractéristics for acces to.(stored in subskeys).

So I need to create objects whose the name is "AX001" or "AX006", or .... to
be able to drive them
How can i do to make something like :
"Machine_Tools <Variable> = new Machine_Tools(); " where <Variable> content
the name of the Machine_Tool object , so that i could make
"AX001.start()"
or other public method of this object in my program ?
How can i do to execute the inside of a variable ?

Thank a lot for your help ...

Bruno
 
Back
Top