How can I create an empty one-dimensional string array?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- How to get the maximum of more than 2 numbers in V
- 'System.Threading.ThreadAbortException' in
- Faster loop: foreach vs some (performance of jsper
Something like:
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:
And then you can point it at a properly-sized array of Strings later:
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:
And then to get an array from that List:
@Mark
Thanks for the correction.
try this Dim Arraystr() as String ={}
The array you created by
Dim s(0) As String
IS NOT EMPTYIn 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:
or
Dim strEmpty(-1) As String
You don't have to include String twice, and you don't have to use New.
Either of the following will work...
Not sure why you'd want to, but the C# way would be
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
and converting it to an array (if you really need it as an array) with