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)
   pass array name to class method?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: pass array name to class method?  (Read 317 times)
vlorch


pass array name to class method?
« on: Jan 2nd, 2005, 10:20pm »

hi all, i am new to this and would appreciate a pointer in the right direction.
i am trying to write a class method, that can accept int and and int[] array as argument. but i'm having trouble.
 
say my method is defined
 
void myMethod (int x, int z, int[] s) {
     s[1] = x + z;
}
 
then i call the class  
 
myClass.myMethod(1, 2, myArray[])
 
i'm getting told that an expression is expected after the myArray[] call. what am i doing wrong.  
 
thanks in advance to those that can show me the error of my ways.
 
vlorch
 
st33d

WWW Email
Re: pass array name to class method?
« Reply #1 on: Jan 2nd, 2005, 11:27pm »

Funny you should need to pass a whole array to a function. I only need to do that when doing a deep copy of the array to expand it. Just a reference to a single integer as opposed to asking for the whole array would do, depends on what you're trying to do though.
 
I've provided an example of a class passing arrays in and out of a method. I don't get any grief because I don't stick the array brackets "[]" in when calling the function. I hope this is of some help.
 
Code:

one thing;
int [] anotherArray = new int[1];
void setup(){
thing = new one();
anotherArray[0] = 1;
}
void draw(){
thing.meth(3,thing.myArray);
thing.meth(2,anotherArray);
println(thing.myArray[0]);
println(anotherArray[0]);
}
class one {
int [] myArray = new int[1];
one(){
myArray[0] = 1;
}
void meth (int x, int[] s) {
     s[0] = x;
}
}
 

I could murder a pint.
vlorch


Re: pass array name to class method?
« Reply #2 on: Jan 3rd, 2005, 10:35am »

thanks. that was it, dropping the '[]' when calling it. *smacks forehead*
 
is it funny to be doing this? i'm not intending to create an array with the method, just a reference to an existing one. anyway, seems to be doing what i want now. on to the next hurdle
 
st33d

WWW Email
Re: pass array name to class method?
« Reply #3 on: Jan 3rd, 2005, 1:40pm »

I was just referring to the example you gave. My programs are so short I never have to pass arrays to functions anyway.
 
Code:

//funny
void meth1(int x, int[] y){
y[0] += x;
}
//not funny
void meth2(int x, int y)
y += x;
}
//not funny
void meth3(int x int[] y){
for (int i = 0; i < y.length; i++){
y[i] += x;
}
}
 

I could murder a pint.
Pages: 1 

« Previous topic | Next topic »