We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › help using string as name in constructor
Page Index Toggle Pages: 1
help using string as name in constructor (Read 450 times)
help using string as name in constructor
Apr 22nd, 2009, 9:47am
 
Hi

I am wondering if there is a way to use a string as an object is being instantiated. Say I am cycling through a list of entries in a database and I create a string on each pass that represents a label of some sort, is there a way to use this string as the name of the object?

scenari owould look roughly like this:

for (int j=1; j<numberOfItems; j++){
   String label = anchorsTable.getString(j,0);
   Float x = anchorsTable.getFloat(j,1);
   Float y = anchorsTable.getFloat(j,2);
   
   label = new Anchor(ps, x, y, label);
   z.anchors.add(label);
 }

I want to "name" the object given by the string....
is this possible?

thanks!
Re: help using string as name in constructor
Reply #1 - Apr 23rd, 2009, 1:27am
 
I am not sure of what you mean. You want to create an object (instantiation of a given class) depending on a string obtained from an external source
It is not far from the problem asked in searching arraylist (later in the thread), but also from serialization.
So either you make an array of string -> object pairs, or you use reflection to create the object from a string.
Page Index Toggle Pages: 1