从 VB.NET 对象数组列表中检索数据 [英] Retrieving data from a VB.NET arraylist of objects

查看:34
本文介绍了从 VB.NET 对象数组列表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从对象的 ArrayList (.NET 1.1 Framework) 中检索正确的值:

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 事件代码包含:

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)

我总是得到列表中最后一个值的 ItemNo.

I always get the ItemNo of the last value in the list.

我错过了什么?

推荐答案

放入如下代码:

Dim AnItemObj As New ItemInfo

inside 将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆