views:

830

answers:

5

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

// I am getting error . How do I initialize it?

A: 

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

Does for me....

Tony

Tony Lambert
A: 

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

BFree
A: 

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

foson
A: 

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

Learner
A: 

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}};
        }
BFree