Learner
Learner

Reputation: 990

Initialize multidimensional array

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

I am getting error . How do I initialize it?

Upvotes: 0

Views: 5734

Answers (5)

BFree
BFree

Reputation: 103770

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

Upvotes: 2

Learner
Learner

Reputation: 990

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

Upvotes: 0

foson
foson

Reputation: 10227

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

Upvotes: 0

BFree
BFree

Reputation: 103770

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

Upvotes: 0

AnthonyLambert
AnthonyLambert

Reputation: 8830

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

Does for me....

Tony

Upvotes: 0

Related Questions