0
 Checkbox[,] checkArray = new Checkbox[2, 3]{{checkbox24,checkboxPref1,null},                                    {checkbox23,checkboxPref2,null}};

I am getting error . How do I initialize it?

2
  • What is the error you're getting? The statement looks fine enough on first glance at least... Commented Jan 7, 2009 at 15:50
  • field initializer can not reference the non static field, method property. Commented Jan 7, 2009 at 15:53

5 Answers 5

2

OK, I think I see what's happening here. You're trying to initialize an array at a class level using this syntax, and one of the checkboxes is also a class level variable? Am I correct?

You can't do that. You can only use static variables at that point. You need to move the init code into the constructor. At the class level do this:

 CheckBox[,] checkArray;

Then in your constructor:

public Form1()
        {
            InitializeComponent();
            checkArray = new CheckBox[2, 3] { { checkbox24,checkboxPref1,null}, {checkbox23,checkboxPref2,null}};
        }
Sign up to request clarification or add additional context in comments.

Comments

0

int[,] myArray; myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}};

Does for me....

Tony

Comments

0

The only thing I see wrong with your code is that it's a CheckBox, not a Checkbox. Capital 'B'.

Comments

0

Make sure all of your variables (checkbox24, checkboxPref1, checkbox23, and checkboxPref2) are of type CheckBox

Comments

0

Initialized each element of array in the constructor and it worked. .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.