Index array out of bounds. Why?

earvinnill

New member
Joined
Aug 16, 2016
Messages
4
Programming Experience
5-10
Here is my code

VB.NET:
        Dim num() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
        Dim temp As String = ""
        Dim str As String = txtReading.Text
        For x As Integer = 0 To str.Length


            For y As Integer = 0 To num.Length


                If str(x) = num(y).ToString Then ------> Error
                    temp += txtReading.Text.IndexOf(x).ToString
                End If


            Next


        Next
 
Think about it. What is 'num.Length'? It's 10, right? So you're going from index 0 to 10. Does that make sense? 0 to 10 is 11 indexes but there are only 10 elements, hence the index out of range. Take a look at any working example of a For loop and an array or collection and you'll see immediately what your error is, or simply think about what it means that arrays and collections are zero-based.
 
Back
Top