I am trying to retrieve the correct value from an ArrayList of objects (.NET 1.1 Framework):
I have the following defined:
Public AlList As New ArrayList
Public Class ItemInfo
Public ItemNo As Int16
Public ItemType As String
Public Reports As Array
Public PDFs As Array
End Class
The form_load event code contains:
Dim AnItemObj As New ItemInfo
Then a loop that includes:
AnItemObj.ItemNo = AFile.RecordId
AnItemObj.ItemType = temp
AlList.Add(AnItemObj)
So I should now have an ArrayList of these objects, however if I try to retrieve the data:
MsgBox(AlList(5).ItemNo)
I always get the ItemNo of the last value in the list.
What am I missing?
Put the following code:
inside the loop which adds AnItemObj to the list.
When you add a reference type to a list, you are only adding the reference, not the value.
This means that if you add 10 times the same instance to a list, it will add 10 times the same reference to the list. But if afterward you still have a reference to this instance you can modify its properties and as all 10 entries in the list point to the same reference in memory, all 10 entries will be modified.