How to: Create an Array with No Elements
An array with no elements is also called a zero-length array. A variable holding a zero-length array does not have the value Nothing.
You might need to create a zero-length array under the following circumstances:
Your code needs to access members of the Array class, such as Length or Rank, or call a Visual Basic function such as UBound Function (Visual Basic), without risking a NullReferenceException exception.
You want to keep the consuming code simpler by not having to check for Nothing as a special case.
Your code interacts with an application programming interface (API) that requires you to pass a zero-length array to one or more procedures, or that returns a zero-length array from one or more procedures.
To create an array with no elements
Declare one of the array's dimensions to be -1. The following example declares a variable to hold an array with elements of the String Data Type (Visual Basic), but initially sets it to be empty.
Dim twoDimensionalStrings(-1, 3) As String
Following the execution of this statement, the array in variable twoDimensionalStrings is two-dimensional with length 0. It is empty, but it nonetheless exists. Therefore, a variable pointing to the array is not equal to Nothing. Subsequently, you can create a nonempty array and assign it to twoDimensionalStrings.
In contrast, the following example declares an array variable that does not initially point to any array.
Dim twoDimStrings( , ) As String
Unlike twoDimensionalStrings in the earlier example, the variable twoDimStrings has the value Nothing.
See Also
Tasks
How to: Declare an Array Variable
How to: Create an Array with More Than One Dimension
How to: Create an Array of Arrays
How to: Create an Array with Mixed Element Types
How to: Initialize an Array Variable
Concepts
Overview of Arrays in Visual Basic