0

This is how I am trying to implement my PictureBox arrays:

    PictureBox[] column0 = new PictureBox[6];
    PictureBox[] column1 = new PictureBox[6];
    PictureBox[] column2 = new PictureBox[6];
    PictureBox[] column3 = new PictureBox[6];
    PictureBox[] column4 = new PictureBox[6];
    PictureBox[] column5 = new PictureBox[6];
    PictureBox[] column6 = new PictureBox[6];

    PictureBox[][] columns = 
            new PictureBox[][] 
            { column0, column1, column2, column3, column4, column5, column6 };

When I try to make the array of arrays, I get this error:

A field initializer cannot reference the non-static field method, or property 'Connect_Four_Server.Server.column0'

and same errors for column1, column2, etc.

How does one declaring an array of arrays correctly in this situation?

3
  • 1
    The problem is not creating the array of arrays, maybe you have some server connection problems... Commented Dec 8, 2012 at 5:19
  • Yep, is says Server.column0? Commented Dec 8, 2012 at 5:20
  • I get this error before I even compile. I get the red line beneath the column0, column1, etc. doing this offline right now, ignore the name server for now Commented Dec 8, 2012 at 5:28

1 Answer 1

1

There's nothing wrong with the above declaration per se, but you can't use it like that if you are declaring fields in a class. As the error states, a field initializer (in your case columns) can't reference other non static fields (in your case column0, column1...).

There are 2 approaches you can take:

Either don't declare intermediate fields and do it all in one piece (best unless you actually need direct references to columnX):

PictureBox[][] columns =
        new PictureBox[][] 
        { 
            new PictureBox[6], 
            new PictureBox[6], 
            new PictureBox[6], 
            new PictureBox[6], 
            new PictureBox[6], 
            new PictureBox[6], 
            new PictureBox[6] 
        };

Or put the initialization of columns into the constructor:

PictureBox[] column0 = new PictureBox[6];
PictureBox[] column1 = new PictureBox[6];
PictureBox[] column2 = new PictureBox[6];
PictureBox[] column3 = new PictureBox[6];
PictureBox[] column4 = new PictureBox[6];
PictureBox[] column5 = new PictureBox[6];
PictureBox[] column6 = new PictureBox[6];

PictureBox[][] columns;

public Server()
{
    columns = new PictureBox[][] { column0, column1, column2, column3, column4, column5, column6 };
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.