FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Using String to choose an object
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Using String to choose an object  (Read 331 times)
inadaze


Using String to choose an object
« on: Dec 31st, 2004, 9:44pm »

Is it possible to choose an object that comes before the dot operator by entering a String variable?
 
ex:
 
myObject1 obj1 = new myObject1();
myObject2 obj2 = new myObject2();
 
String name = obj2;
 
name.someMethod();
 
Or something that does something similar?
Thanks
Jay
 
st33d

WWW Email
Re: Using String to choose an object
« Reply #1 on: Jan 2nd, 2005, 8:21pm »

A String is effectively an object with it's own methods (such as .substring and .equals). The script reader ends up thinking you're trying to access a method of the String.
 
However you can make an extention of another class a part of an existing object array it seems.
 
I'm not sure as to the flexibility of this though. It wasn't happy about me introducing new variables before the constructor in the extention.
 
If the language allows you could just access different indexes in the array and access different objects of the same order hopefully. Although someone else may have a more straightforward solution.
 
Code:

one [] thing = new one[2];
void setup(){
thing[0] = new one();
thing[1] = new two();
}
void draw(){
println (thing[0].i);
println (thing[1].ii);
}
class one {
int i = 1;
int ii;
one(){
}
}
class two extends one {
two(){
ii = 2;
}
}
 

I could murder a pint.
toxi

WWW
Re: Using String to choose an object
« Reply #2 on: Jan 5th, 2005, 12:53pm »

i don't think this is what inadaze meant really. i think he was more referring to the dynamic programming approach common in losely typed languages, like JS/AS/python etc. something like this is not as straightforward to achieve in java as it's strongly typed. this is in itself forces people to write code which can be checked for validity at compile time and so java apps tend to be more stable.
 
to achieve what you're trying to do in a clean, valid java way, you'd have to write a bit of configuration code to satisfy the nagging compiler (and here read nagging as something positive!).
 
so here goes one possible method, another one would involve Java Reflection and is a lot more messy, so i'd not recommend it.
 
Code:
/*
 * dynamic references demo using java interfaces
 * author: info@toxi.co.uk
 *
*/
 
MyAPICollection coll;
 
void setup() {
  coll = new MyAPICollection();
  coll.addObject("obj1",new MyClass1());
  coll.addObject("obj2",new MyClass2());
   
  // demonstrate dynamic reference possibilities
  for(int i=1; i<3; i++) {
    coll.getObject("obj"+i).someMethod();
    coll.getObject("obj"+i).anotherMethod();
  }
}
 
/*
 * declare an interface
 * include all method declarations which
 * have to be present in classes which implement this interface
 *
*/
 
interface MyAPI {
  void someMethod();
  void anotherMethod();
}
 
/*
 * here're some classes which make use of the MyAPI interface
 * all methods defined in MyAPI need to be implemented
 *
*/
 
class MyClass1 implements MyAPI {
  public void someMethod() {
    println("MyClass1 someMethod() called");
  }
  public void anotherMethod() {
    println("MyClass1 anotherMethod() called");
  }
}
 
class MyClass2 implements MyAPI {
  public void someMethod() {
    println("MyClass2 someMethod() called");
  }
  public void anotherMethod() {
    println("MyClass2 anotherMethod() called");
  }
}
 
/*
 * this is our magic utility class to be able
 * to call objects via string IDs
 * this class also ensures type safety, meaning  
 * only classes which implement MyAPI can be added
 * to this collection
 * returned objects will automatically be recast
 * to the type of MyAPI
 *
*/
class MyAPICollection {
  Hashtable index=new Hashtable();
 
  void addObject(String id, MyAPI newClass) {
    index.put(id,newClass);
  }
 
  MyAPI getObject(String id) {
    return (MyAPI)index.get(id);
  }
}
 

http://toxi.co.uk/
Pages: 1 

« Previous topic | Next topic »