Question

Locked

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

By Slayer_ ·
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?

This conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Interesting...

by cmiller5400 In reply to Array index out of bounds ...

"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 )... Can you post some code?

Collapse -

Here is the code

by Slayer_ In reply to Array index out of bounds ...

<pre>
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
</pre>

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

Collapse -

I think it had something to do with my serialized file

by Slayer_ In reply to Array index out of bounds ...

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.

Back to Software Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums