Question Locked

Array index out of bounds error, VB.net - Solved

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

The trick is, its happening in a for each loop, so how is this possible?

Its a List(of t) type. Count shows 678 elements, however, if you try and access 677 it throws array index out of bounds. If you try to for each through it, on the last element it throws the same error.

What am I doing wrong?

4 replies

by cmiller5400

"For each" will only loop on the elements that are in the array, so I'd assume that it shouldn't generate an out of bounds error (well you know what they say about assuming :D )... Can you post some code?

by slayer_

Dim dc As DetectionCache
                Dim dcToDelete As New ArrayList
                Try
                    For Each dc In aDestinationCache
                        If dc.Expired = True Then
                            dcToDelete.Add(dc)
                        End If
                    Next
                Catch ex As Exception
                    Throw ex
                End Try

DetectionCache is a custom class that holds keys, values, and an expiry date/time
aDestinationCache is a List(of DetectionCache)

by slayer_

aDestinationCache is serialized and loaded and written to a file. When I emptied the file, it became happy.

Oddly, it was also erroring on query results from Linq queries when I for each'ed through the results.

This discussion is closed to new replies.