VB.NET Empty String Array
Dim strEmpty(-1) As String
The array you created by Dim s(0) As String
IS NOT EMPTY
In VB.Net, the subscript you use in the array is index of the last element. VB.Net by default starts indexing at 0, so you have an array that already has one element.
You should instead try using System.Collections.Specialized.StringCollection
or (even better) System.Collections.Generic.List(Of String)
. They amount to pretty much the same thing as an array of string, except they're loads better for adding and removing items. And let's be honest: you'll rarely create an empty string array without wanting to add at least one element to it.
If you really want an empty string array, declare it like this:
Dim s As String()
or
Dim t() As String
VB is 0-indexed in array declarations, so seomthing like Dim myArray(10) as String
gives you 11 elements. It's a common mistake when translating from C languages.
So, for an empty array, either of the following would work:
Dim str(-1) as String ' -1 + 1 = 0, so this has 0 elements
Dim str() as String = New String() { } ' implicit size, initialized to empty