Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 555
oscilatingcretin Avatar asked Feb 20 '13 21:02

oscilatingcretin


People also ask

How do you declare a multidimensional array in Visual Basic?

In visual basic, Multidimensional Arrays can be declared by specifying the data type of an elements followed by the brackets () with comma (,) separator. Following are the examples of creating two or three-dimensional arrays in visual basic programming language.

How do you declare a multidimensional array?

A multi-dimensional array can be termed as an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. The general form of declaring N-dimensional arrays is: data_type array_name[size1][size2]....


1 Answers

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

like image 130
Oded Avatar answered Oct 22 '22 19:10

Oded