oscilatingcretin
oscilatingcretin

Reputation: 10919

How to I declare and initialize a multidimensional array in VB.NET?

I want to do this:

Dim Numbers As Integer()() = {{1}, {2}, {3}, {4, 5, 6, 7}}

The IDE's underlining 4, 5, 6, 7 and saying Array initializer has 3 too many elements. What am I doing wrong?

Upvotes: 12

Views: 11175

Answers (1)

Oded
Oded

Reputation: 498904

The following should work:

Dim Numbers As Integer()() = {({1}), ({2}), ({3}), ({4, 5, 6, 7})}

As documents in Arrays in Visual Basic:

You can avoid an error when you supply nested array literals of different dimensions by enclosing the inner array literals in parentheses. The parentheses force the array literal expression to be evaluated, and the resulting values are used with the outer array literal

Upvotes: 19

Related Questions