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)
   Vector of objects
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Vector of objects  (Read 757 times)
gabon

WWW
Vector of objects
« on: Dec 20th, 2004, 10:27pm »

Hi guys I'm trying to use Vectors and I found a problem.
 
Considering a class like:
 
Code:

class RefObj
{
 int id;
 color col;
 //
 RefObj(int tid, color tcol){
  id=tid;
  col=tcol;
 }
}

 
and a population like:
 
Code:

RefObj obj;
for(int i=0; i<10; i++){
 obj=new RefObj(i,pixels[i]);
 testVect.add(obj);
}

 
Hot to have access to a property of the object?
 
Code:

for(int i=0; i<10; i++){
 println(testVect.get(i).id);
}

 
It says: no field named "id" was found in type "java.lang.Object".
 
 
Thx for any help, chr
 
fjen

WWW
Re: Vector of objects
« Reply #1 on: Dec 20th, 2004, 10:43pm »

just "cast" it back to what it's supposed to be (... or is, actually).
 
side note: "obj" is not a good name since it seems to be used internally by processing.
 
and: you don't need to create an object in a variable to put that into the vector. you can directly create it inside when doing vec.add(new myObj());
 
Code:

class RefObj
{
  int id;
  color col;
  RefObj(int tid, color tcol){
    id=tid;
    col=tcol;
  }
}
Vector testVect;
void setup()
{
  testVect = new Vector();
  RefObj obj;
}
void draw()
{
  for(int i=0; i<10; i++){
    testVect.add( new RefObj(i,pixels[i]) );
  }
  for(int i=0; i<10; i++){
    println(((RefObj)testVect.get(i)).id);
  }
}

 
/F
 
gabon

WWW
Re: Vector of objects
« Reply #2 on: Dec 21st, 2004, 12:19am »

yes, the cast... I'm happy to come back on these strongly typed rules
 
Thank u florian, chr
 
fry


WWW
Re: Vector of objects
« Reply #3 on: Dec 21st, 2004, 2:34am »

on Dec 20th, 2004, 10:43pm, fjen wrote:
side note: "obj" is not a good name since it seems to be used internally by processing.

not that i know of, is it giving you an error
 
fjen

WWW
Re: Vector of objects
« Reply #4 on: Dec 21st, 2004, 3:19am »

ha, right! you got me there. i got fooled by the error-message. should have read it 'til the end ...
 
"... However there is an accessible field ..."
 
/F
 
Pages: 1 

« Previous topic | Next topic »