How do i add to a simple array in vb.net

…衆ロ難τιáo~ 提交于 2019-12-10 12:00:58

问题


If I have

Dim a As String() = ("One,Two").Split(",")

How can I add to that string ?


回答1:


The easiest way is to convert it to a List and then add.

Dim a As List(Of String) = ("One,Two").Split(",").ToList
a.Add("Three")

or if you really want to keep an array.

    Dim a As String() = ("One,Two").Split(",")
    Dim b as List(Of String) = a.ToList
    b.Add("Three")
    a=b.ToArray

And here is something really outside the box:

a = (String.Join(",", a) & ",Three").Split(",")



回答2:


For a different approach, try:

Dim a As String() = ("One,Two").Split(CChar(","))
Debug.Print(CStr(UBound(a)))
ReDim Preserve a(9)
Debug.Print(CStr(UBound(a)))

The output to the immediate window is:

1
9

Note: I have had to slightly change your original line because I always use Option Strict On which does not permit implicit conversions.



来源:https://stackoverflow.com/questions/11421716/how-do-i-add-to-a-simple-array-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!