|
Array problem -- zombek --
Hi. I have a multidimensional array of "Square" objects: private Square _board; In the constructor of the class which _board is a member I have the following code: for(int i = 0; i < BOARD_WIDTH; i++){ for(int j = 0; j < BOARD_HEIGHT; j++){ _board = new Square(); } } In an other method I want to get to one of the Square class' fields like this: if (_board .State == Square.State.OCCUPIED){...} But I get the following error: An object reference is required for the nonstatic field 'square.State.get' I don't know what's wrong. |
|
-- Bort --
Hi zombek, What is State defined as? I'm guessing that you want it to be a static property or enumeration on the Square class, right? If that is correct, then you need to declare it as "static" like this: public static enum State { on, off } or something like that. The problem is that the statement: .State == Square.State.OCCUPIED){...} is trying to reference the State property on the Square class - which it cannot see. Bort zombek wrote: Hi. I have a multidimensional array of "Square" objects: private Square _board; In the constructor of the class which _board is a member I have the following code: for(int i = 0; i < BOARD_WIDTH; i++){ for(int j = 0; j < BOARD_HEIGHT; j++){ _board = new Square(); } } In an other method I want to get to one of the Square class' fields like this: if (_board .State == Square.State.OCCUPIED){...} But I get the following error: An object reference is required for the nonstatic field 'square.State.get' I don't know what's wrong. |
|
-- Bort --
Hi zombek, I wrote a reply, but I don't see it, so here goes again: This line .State == Square.State.OCCUPIED){...} is looking for a static property on the Square class, but cannot see it. How is it defined? Bort zombek wrote: Hi. I have a multidimensional array of "Square" objects: private Square _board; In the constructor of the class which _board is a member I have the following code: for(int i = 0; i < BOARD_WIDTH; i++){ for(int j = 0; j < BOARD_HEIGHT; j++){ _board = new Square(); } } In an other method I want to get to one of the Square class' fields like this: if (_board .State == Square.State.OCCUPIED){...} But I get the following error: An object reference is required for the nonstatic field 'square.State.get' I don't know what's wrong. |