56

How can I create an empty one-dimensional string array?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
YonahW
  • 15,790
  • 8
  • 42
  • 46
  • @Pacane Please [read this](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles). – Nikita Volkov Jun 11 '13 at 14:05

11 Answers11

49

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
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
  • 3
    I always do Dim str() as String = New String() { } because i feel that it is the best way to see that it is an EMPTY String array "{ }" – Ignacio Soler Garcia Nov 18 '09 at 15:34
  • 2
    Mark, Dim str(0) produces an array with a length of 1 with a NULL at index 0. The second, Dim str()...{ } produces an empty array with a length of zero as YonahW wanted. – Randy Eppinger Aug 18 '10 at 13:06
  • @RandyEppinger is correct (and has been for the last 3 years...). str(0) does produce an array of length 1 (being 0-indexed means the length is +1 the declaration). I should've put str(-1) (which, when +1 gives you a length of 0). – Mark Brackett Jun 11 '13 at 14:02
  • I'm reluctant to remove half of your answer, but it is however redundant since Chris has now revised his answer. Maybe this could be reduced to only answer the question instead? Sorry for bumbing such an old thread. – default Oct 18 '17 at 14:15
40

Dim strEmpty(-1) As String

G8RDA
  • 440
  • 5
  • 4
  • 11
    Upvote because it is correct. However, for clarity, I prefer Dim strEmpty() As String = New String() {} as Mark offers and SoMoS endorses. However, Mark describes two techniques that are not equivalent...see that comment. – Randy Eppinger Aug 18 '10 at 12:56
  • 11
    Update...We found the following to be equivalent and it has less ceremony: Dim myArray() = New String() {} – Randy Eppinger Aug 28 '10 at 21:09
  • or to make it even more confusing - `Dim s$(-1)` or `Dim s$() = {}` – Slai Aug 01 '16 at 15:52
  • 2
    A simple Dim empty As String = {} is the most elegant version – Carl Onager May 17 '17 at 11:18
  • Dim s$() = {} is hated by VB converters... The (-1) syntax is more VBish IMHO. – Larry Jun 07 '19 at 09:57
9

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
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 4
    Joel, this doesn't provide the requested behavior. Each of these, t and s are Nothing. YonahW wanted an empty array which GR8DA's solution provides, although I prefer: Dim strEmpty() As String = New String() {} – Randy Eppinger Aug 18 '10 at 13:00
8

You don't have to include String twice, and you don't have to use New.
Either of the following will work...

Dim strings() as String = {}
Dim strings as String() = {}
JustinMichel
  • 581
  • 7
  • 6
8

Something like:

Dim myArray(9) as String

Would give you an array of 10 String references (each pointing to Nothing).

If you're not sure of the size at declaration time, you can declare a String array like this:

Dim myArray() as String

And then you can point it at a properly-sized array of Strings later:

ReDim myArray(9) as String

ZombieSheep is right about using a List if you don't know the total size and you need to dynamically populate it. In VB.NET that would be:

Dim myList as New List(Of String)
myList.Add("foo")
myList.Add("bar")

And then to get an array from that List:

myList.ToArray()

@Mark

Thanks for the correction.

Chris Zwiryk
  • 1,321
  • 9
  • 9
4

Another way of doing this:

Dim strings() As String = {}

Testing that it is an empty string array:

MessageBox.Show("count: " + strings.Count.ToString)

Will show a message box saying "count: 0".

2

A little verbose, but self documenting...

Dim strEmpty() As String = Enumerable.Empty(Of String).ToArray
Scott Mitchell
  • 125
  • 1
  • 6
1

Not sure why you'd want to, but the C# way would be

string[] newArray = new string[0];

I'm guessing that VB won't be too dissimilar to this.

If you're building an empty array so you can populate it with values later, you really should consider using

List<string>

and converting it to an array (if you really need it as an array) with

newListOfString.ToArray();
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
1
Dim array As String() = Array.Empty(Of String)
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
0

try this Dim Arraystr() as String ={}

Lisa
  • 3
  • 8
0

I know this is an old thread, but another way to create an empty one-dimensional string array is to use the Array.Empty<T> method.

E.g.,

VB.NET

Dim emptyStringArray() As String = Array.Empty(Of String)()

C#

string[] emptyStringArray = Array.Empty<string>()
LarsTech
  • 80,625
  • 14
  • 153
  • 225
Joshua Barker
  • 113
  • 1
  • 8