从对象的数组列表VB.NET中检索数据 [英] Retrieving data from a VB.NET arraylist of objects
问题描述
我想从对象的ArrayList获取正确的值(.NET 1.1框架):
I am trying to retrieve the correct value from an ArrayList of objects (.NET 1.1 Framework):
我有以下定义:
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
Form_Load事件code包含:
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)
所以,我现在应该有这些对象的ArrayList,但是,如果我尝试检索数据:
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.
我是什么失踪?
推荐答案
将下面的code:
Dim AnItemObj As New ItemInfo
在这增加AnItemObj到列表中的循环。
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.
这意味着,如果添加10倍同一实例的列表,它将相同的附图10次添加到列表中。但是,如果之后你仍然有参考这个情况下,你可以修改它的属性,并在列表指向内存中相同的参考所有10项,所有10个条目将被修改。
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.
这篇关于从对象的数组列表VB.NET中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!