VB.NET Empty String Array

.NetArraysvb.net

.Net Problem Overview


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

.Net Solutions


Solution 1 - .Net

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

Solution 2 - .Net

Dim strEmpty(-1) As String

Solution 3 - .Net

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

Solution 4 - .Net

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.

Solution 5 - .Net

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() = {}

Solution 6 - .Net

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".

Solution 7 - .Net

A little verbose, but self documenting...

Dim strEmpty() As String = Enumerable.Empty(Of String).ToArray

Solution 8 - .Net

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();

Solution 9 - .Net

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

Solution 10 - .Net

try this Dim Arraystr() as String ={}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYonahWView Question on Stackoverflow
Solution 1 - .NetMark BrackettView Answer on Stackoverflow
Solution 2 - .NetG8RDAView Answer on Stackoverflow
Solution 3 - .NetJoel CoehoornView Answer on Stackoverflow
Solution 4 - .NetChris ZwirykView Answer on Stackoverflow
Solution 5 - .NetJustinMichelView Answer on Stackoverflow
Solution 6 - .NetMichael JohnsonView Answer on Stackoverflow
Solution 7 - .NetScott MitchellView Answer on Stackoverflow
Solution 8 - .NetZombieSheepView Answer on Stackoverflow
Solution 9 - .NetDennis SpadeView Answer on Stackoverflow
Solution 10 - .NetLisaView Answer on Stackoverflow