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 › How do I refer to an array in a seperate class
Page Index Toggle Pages: 1
How do I refer to an array in a seperate class? (Read 454 times)
How do I refer to an array in a seperate class?
Oct 22nd, 2006, 10:04am
 
Hi,

I am defining a class as an array of objects:

MyClass [] myCass = new MyCass[9];

class MyClass{
void parameters(GLOBAL world){
}
}

Inside the class called GLOBAL, how do I then refer to the array of objects that are in MyClass? This doesn't work:

GLOBAL world;

class GLOBAL {
void main(MyClass[] myClass[]) {

}
}
Re: How do I refer to an array in a seperate class
Reply #1 - Oct 22nd, 2006, 2:07pm
 
I would advise against calling Global "GLOBAL". All capitals are traditionally used for constants - things that don't change. You risk confusing people who are reading your code.

I assume it's a typo that you want the array of objects in MyClass.

Here's how to do it:
Code:

MyClass [] myClass = new MyClass[9];
Global world;

void setup(){
world = new Global();
world.main(myClass);
}

class MyClass{
MyClass(){
}
}

class Global{
Global(){
}
void main(MyClass [] reference){
// Either access the array by passing a reference
println(reference.length);
// Or: myClass is a global variable, accessible to the whole applet
println(myClass.length);
}
}
Page Index Toggle Pages: 1