Udostępnij za pośrednictwem


Arrays cannot be declared with 'New'

The New keyword can appear only in the initialization part of an array declaration. This means New must be on the right side of the equal sign (=) so it can create a new array type to be assigned to the array variable.

The shortcut for class initialization is not available for arrays. The following two code lines are both valid and are equivalent because they initialize an object from a class.

Dim formA as Form = New Form
Dim formA as New Form

However, array initialization cannot use the same shortcut as class initialization.

Note that the New clause for an array must contain both parentheses, (), and braces, {}. The parentheses specify that the new type is an array, and the braces supply the initialization values. The compiler requires the braces even if they are empty, that is, even if you are not initializing any of the array values.

Error ID: BC30053

To correct this error

  • Replace a statement such as Dim myDates() As New Date with a statement such as Dim myDates() As Date = New Date() {}.

See Also

Tasks

How to: Initialize an Array Variable

Other Resources

Arrays in Visual Basic