I need to add items to an arraylist until the count gets to, for example, 100 and then read them on a Last in first out basis.
ow do I achieve this?
Printable View
I need to add items to an arraylist until the count gets to, for example, 100 and then read them on a Last in first out basis.
ow do I achieve this?
First off, in VB 2005, you should never use the ArrayList, if you need a collection you should use the List(Of T).
However, in this scenario, you should use a collection that is created for this purpose:VB.NET Code:
System.Collections.Generic.Stack(Of T)
Before add an item to your array list, you check the Count property to see if it reaches the limit. If it doesn't, add the item. Else, don't add. Now to read the items LIFO, you just need a For loop running backward from arraylist.count - 1 to 0.
EDIT: If you can use something other than an arraylist, I strongly suggest you to use a stack as Atheist said.