Alex Gordon
Alex Gordon

Reputation: 60691

vb.net: index was out of range error

can someone please help me. why does this return an error:

    Dim stuff As New System.Collections.ArrayList()
    Dim i As Integer
    i = 1

    Dim split As String() = temp_string.Split(",")
    For Each s As String In split
        If s.Trim() <> "" Then
            stuff(i) = s
            i = i + 1
        End If
    Next s

the stuff(i)=2 line is returning the mentioned error

Upvotes: 0

Views: 1459

Answers (2)

eugeneK
eugeneK

Reputation: 11116

Use stuff.Add(i) instead of that, you accessing not an array but a list which doesn't have an index upon creation only after you assign values you can access it's indexes as an array.

Upvotes: 5

Chris Kloberdanz
Chris Kloberdanz

Reputation: 4536

Looks like it could be an off by one error. What is i initialized to? 0 or 1?

Upvotes: 1

Related Questions