Initialize multidimensional array

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

      

I get an error. How do I initialize it?

0


source to share


5 answers


Okay, I think I can see what's going on here. Are you trying to initialize an array at the class level using this syntax and one of the checkboxes is also a class level variable? I'm right?

You cannot do this. You can only use static variables at this point. You need to move your initialization code to the constructor. At the class level, do the following:

 CheckBox[,] checkArray;

      



Then in your constructor

public Form1()
        {
            InitializeComponent();
            checkArray = new CheckBox[2, 3] { { checkbox24,checkboxPref1,null}, {checkbox23,checkboxPref2,null}};
        }

      

+2


source


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

Does for me ....



Tony

0


source


The only thing I can see in your code is a CheckBox, not a checkbox. Capital "B".

0


source


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

0


source


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

0


source







All Articles