Array declared as for loop control variable cannot be declared with an initial size

A For Each loop uses an array as its element iteration variable but initializes that array.

The following statements show how this error can be generated.

Dim arrayList As New List(Of Integer())
For Each listElement() As Integer In arrayList
For Each listElement(1) As Integer In arrayList

The first For Each statement is the correct way to access elements of arrayList. The second For Each statement generates this error.

Error ID: BC32039

To correct this error

  • Remove the initialization from the declaration of the element iteration variable.

See Also

Concepts

Overview of Arrays in Visual Basic

Collections in Visual Basic

Reference

For...Next Statement (Visual Basic)