|
Author |
Topic: Accessing arrays in a class structure (Read 866 times) |
|
ouoertheo
|
Accessing arrays in a class structure
« on: May 29th, 2004, 11:54pm » |
|
Im sure this has been brought up before, but I cant seem to find something on it, but I was wondering why I am unable to access arrays when I am making a class? I can access them inside functions and sll, just not in the main classvariable definitions.
|
|
|
|
kevinP
|
Re: Accessing arrays in a class structure
« Reply #1 on: Jun 1st, 2004, 1:18am » |
|
Do you mean this? Code: MyClass o = new MyClass(222); void draw() { println(o.myArray[0]); println(o.myArray[1]); } class MyClass { // fields int[] myArray = new int[3]; myArray[0] = 111; // <-- compile error: "expecting RBRACK, found '0'" // constructor MyClass(int nbr) { myArray[1] = nbr; } } |
|
|
« Last Edit: Jun 1st, 2004, 1:19am by kevinP » |
|
Kevin Pfeiffer
|
|
|
ouoertheo
|
Re: Accessing arrays in a class structure
« Reply #2 on: Jun 3rd, 2004, 3:13am » |
|
yes, precisely that
|
|
|
|
ouoertheo
|
Re: Accessing arrays in a class structure
« Reply #3 on: Jun 6th, 2004, 10:35pm » |
|
is there something else I should be doing instead of this?
|
|
|
|
kevinP
|
Re: Accessing arrays in a class structure
« Reply #4 on: Jun 6th, 2004, 11:03pm » |
|
Hi ouoertheo, on Jun 6th, 2004, 10:35pm, ouoertheo wrote:is there something else I should be doing instead of this |
| I don't know; I was waiting to see if someone else would suggest that this might be a bug. -K
|
Kevin Pfeiffer
|
|
|
kevinP
|
Re: Accessing arrays in a class structure
« Reply #5 on: Jun 11th, 2004, 9:28pm » |
|
Just reflagging this in the hope that Ben or someone else who might know can resolve this question. -K
|
Kevin Pfeiffer
|
|
|
flight404
|
Re: Accessing arrays in a class structure
« Reply #6 on: Jun 11th, 2004, 10:26pm » |
|
Well, this seems to work... Code: MyClass o = new MyClass(222); void draw(){ println(o.myArray[0]); println(o.myArray[1]); } class MyClass{ int[] myArray; MyClass(int nbr) { myArray = new int[3]; myArray[0] = 111; myArray[1] = nbr; } } |
| I don't know if this is an answer that would satisfy you, but it compiled without error. r
|
|
|
|
fry
|
Re: Accessing arrays in a class structure
« Reply #7 on: Jun 12th, 2004, 4:05am » |
|
right, this: Code:class MyClass { // fields int[] myArray = new int[3]; myArray[0] = 111; // <-- compile error: "expecting RBRACK, found '0'" |
| is just plain incorrect syntax, and that's the case for c, c++, java (and therefore processing), etc. my guess is that language designers left it out because the order of how initializations happen outside of code blocks is indeterminate. meaning, that as the code is compiled, there is no way to ensure that the "new int[3]" will happen before the "myArray[0] = 111". when they're part of the program (inside the constructor, as in flight404's example) that has a more specific flow.
|
|
|
|
kevinP
|
Re: Accessing arrays in a class structure
« Reply #8 on: Jun 12th, 2004, 10:41am » |
|
Thanks! Nice to hear an explanation; that pleases the child in me who (still) is always asking, "but why?".
|
Kevin Pfeiffer
|
|
|
ouoertheo
|
Re: Accessing arrays in a class structure
« Reply #9 on: Jul 18th, 2004, 11:41pm » |
|
Ahhh, its good to be back. Thanks for that explanation, I figured the other way out myself, I was just curious as to whether the way asked about that returned the error, was a syntax problem on my side. Which was clearly explained. Thanks
|
|
|
|
|