Array initializer cannot be specified for a non constant dimension; use the empty initializer '{}'

An array initializes a dimension that is not known at compile time.

The following code generates this error.

Dim j As Integer
Dim intArray As Integer = New Integer(1, j) {{0, 100}, {1,101}}

The following code avoids the error.

Dim intArray As Integer = New Integer(1, j) {}
For i As Integer = 0 To j
    intArray(0, i) = i
    intArray(1, i) = 100 + i
Next i

Error ID: BC30949

To correct this error

  • If possible, specify a constant dimension in the array declaration.

  • If you cannot specify a constant dimension, then you must initialize the array using a loop when the nonconstant dimension becomes known.

See Also

Tasks

How to: Initialize an Array Variable

How to: Initialize a Multidimensional Array

Concepts

Overview of Arrays in Visual Basic

Reference

For Each...Next Statement (Visual Basic)