kevin
kevin

Reputation: 15

VB.NET: How to incrementally replace values in String Array

I have a String array with the following values:

dim strArray() as string  
dim newValueFor0Index as string = "Z"  
strArray(0) = "A"  
strArray(1) = "B"  
strArray(2) = "C"`

what i need is a loop or something to replace the values as such:

strArray(0) = "Z"  
strArray(1) = "A"  
strArray(2) = "B"`

I then need to add another index to the strArray() which will hold the last value ie:

strArray(0) = "Z"  
strArray(1) = "A"  
strArray(2) = "B"  
strArray(3) = "C"`

I tried a while loop :

while i < Count

    ni++  
    currentArr(ni) = currentArr(i)  
    i++  
end while  

but this won't work because i am using redundant values

Upvotes: 1

Views: 2385

Answers (1)

Hans Passant
Hans Passant

Reputation: 941635

An array is simply the wrong collection type to do this. It is trivial with a list:

    Dim lst As New List(Of String)
    lst.Add("A")
    lst.Add("B")
    lst.Add("C")
    lst.Insert(0, "Z")

You can always whack it back to an array, if really necessary:

    Dim array() As String = lst.ToArray()

Upvotes: 4

Related Questions