views:

14048

answers:

8

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

A: 

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
+3  A: 

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
+2  A: 

Dim strEmpty(-1) As String

G8RDA
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
Update...We found the following to be equivalent and it has less ceremony:Dim myArray() = New String() {}
Randy Eppinger
A: 

FYI, be careful with ReDim-ing

It's kind of dangerous at times. like goto

chakrit
A: 

Thanks all for your answers.

I ended up doing

Dim s(-1) as String

which is what @G8RDA suggested

I appreciate the quick response

YonahW
+5  A: 

Sorry for the down vote, but I wanted to try to make sure you came back to this question, because 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. That means you have an array that already has one element.

You should try using System.Collections.Specialized.StringCollection or 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
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
+3  A: 

@Chris

Something like:

Dim myArray(10) as String

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

VB is 0-indexed in array declarations, so you'd actually have 11 elements in that array. It's a common mistake when translating from C languages.

As for the question, either of the following would work:

Dim str(0) as String
Dim str() as String = New String() { }
Mark Brackett
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 "{ }"
SoMoS
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
A: 

@Joel

Thank you for pointing out that this is not an empty string array. I actually came back because I found this out when my use crashed.

I actually do not plan on adding any items to the string array and specifically must use a string array.

I am calling an internal method that expects a string array as one of its parameters. I could create another overload to the method that does not expect it, however I thought it might be easier to just pass an empty string array.

It turns out that @G8RDA was correct and I ended up using his method.

Once again thanks to everyone for the responses.

YonahW