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 › Determine Class Position in Array from within
Page Index Toggle Pages: 1
Determine Class Position in Array from within (Read 452 times)
Determine Class Position in Array from within
Feb 24th, 2010, 7:48am
 
I did some searching through the forums, but I wasn't completely sure what to search for. Is it possible to return the identifier of an array of classes from the class itself?

E.G.

Code:

myObject[] group = new myObject[100];

void setup() {
   for(int i = 0; i < group.length; i++) {
       group[i] = new myObject();
   }
}

void draw() {
   for(int i = 0; i < group.length; i++) {
       group[i].update();
   }
}

class myObject {
   myObject() {
       // Constructor Information
   }

   void update() {
       // Print the position of this object
       // in the array group.
   }
}


I don't think this code will compile, but I hope it's sufficient enough to get the point across. If it's not let me know and I'll try to explain myself better. Thanks!
Re: Determine Class Position in Array from within
Reply #1 - Feb 24th, 2010, 7:53am
 
Quote:
myObject[] group = new myObject[100];

void setup() {
  for(int i = 0; i < group.length; i++) {
    group[i] = new myObject(i);
  }
}

void draw() {
  for(int i = 0; i < group.length; i++) {
    group[i].update();
  }
}

class myObject {
  int myIndex;
  myObject(int newIndex) {
    // Constructor Information
    myIndex = newIndex;
  }

  void update() {
    // Print the position of this object
    // in the array group.
    println( myIndex );
  }
}



Works fine. Two other small changes:
- Processing thinks "Object" is a keyword. Don't use it for your own class name.
- When you define a class, use the keyword "class", not "Class". Lowercase 'c'.
Re: Determine Class Position in Array from within
Reply #2 - Feb 24th, 2010, 8:06am
 
Of course! Thanks so much for the quick response. I don't know why I hadn't thought of passing the iterator into the constructor...
Page Index Toggle Pages: 1