1

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

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

5 Comments

the array is coming from a cookie and i am putting it into an array by doing... strArray = cookieValue.split("$")
also if i do as you advise won't the values of the list be Z, B, C? when i need it to be Z, A, B, C
Dim lst As New List(Of String)(cookieValue.Split("$"c)). No, Insert really does insert, not replace.
what does the c signify here?"$"c
@kevon: It's a cast to char. You might want to turn Option Strict to On. Will save you headache if it comes to casting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.