Sunday, March 20, 2011

Initialize multidimensional array

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

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

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

    Does for me....

    Tony

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

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

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

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

0 comments:

Post a Comment