masteroleary
masteroleary

Reputation: 1034

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

If I have

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

How can I add to that string ?

Upvotes: 0

Views: 28086

Answers (2)

Holger Brandt
Holger Brandt

Reputation: 4354

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(",")

Upvotes: 5

Tony Dallimore
Tony Dallimore

Reputation: 12413

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.

Upvotes: 1

Related Questions