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 › Calling array values from a method.
Page Index Toggle Pages: 1
Calling array values from a method. (Read 420 times)
Calling array values from a method.
Oct 24th, 2009, 10:07pm
 
Hello,

When I run the following piece of code and try to call an array created by a method within the class I created I receive the error
"moon.moon cannot be resolved"
How do you properly call the array created by moon.math();

-------------------------------------------------------------------------------

class Test{
 int array_size;  int r1=10;

 Test(){
 }

 void  math(int array_size){
   this.array_size=array_size;
   int[] moon=new int[array_size];
   for (int i=0; i <array_size;i++){
     moon[i]=r1;
     r1+=10;
   }
   for (int q=0;q<array_size;q++){
     println(moon[q]);
   }

 }
}

////////////////////////////////////////////////////////////////////////////
Test moon;
void setup(){
 moon = new Test(){
 };
 moon.math(20);// <----------- this sets the size of the array and prints the varaiables from
                               //the moon array in math()
 
 for (int i=0;i<100;i++){
   println(moon.moon[i]);//    but when I call the array that I created using the math() method
                         //    I recieve the error that that moon.moon cannot be resolved

 }

}
Re: Calling array values from a method.
Reply #1 - Oct 25th, 2009, 1:39am
 
That's because you declare the moon array inside the math() method: this array lives only the duration of the function call.
You must declare it at the same level than the array_size, and initialize it where you did.
Page Index Toggle Pages: 1