vb.net - 将对象添加到数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7727156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:43:25  来源:igfitidea点击:

vb.net - add object to arraylist

vb.netarraysobjectarraylist

提问by Joe

I'm having some trouble adding an object to an arraylist.

我在将对象添加到数组列表时遇到了一些问题。

Basically the object has two properties (file id/name), but I can't figure out how to assign those properties. During runtime it errors out with public member on the object not found.

基本上该对象有两个属性(文件 ID/名称),但我不知道如何分配这些属性。在运行时,它在找不到对象上的公共成员上出错。

Private QueueList As New ArrayList
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
    Dim QueueObj As New Object
    QueueObj.FileID = "Test 1"
    QueueObj.FileName = "Test 2"
    QueueList.Add(QueueObj)
End Sub

I'd also like to know how I can do a loop on the arraylist and access the two properites on each record.

我还想知道如何对数组列表进行循环并访问每个记录上的两个属性。

Thanks!

谢谢!

回答by Joel Coehoorn

You can't just use "Object" for this. You need to build your own class:

你不能只使用“对象”。您需要构建自己的类:

Public Class File
    Public Property FileID As Integer
    Public Property FileName As String
    Public Sub New ()
    End Sub
    Public Sub New(ByVal FileName As String, ByVal FileID As Integer)
        Me.FileID = FileID
        Me.FileName = FileName
    End Sub
End Class

And then build your Queue like this:

然后像这样构建你的队列:

Private QueueList As New ArrayList()
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
    QueueList.Add(New File(FileName, FileID))
End Sub
Public Sub Queue(ByVal FileObj As File)
    QueueList.Add(FileObj)
End Sub

Or, even better, use generics:

或者,更好的是,使用泛型:

Public QueueList As New List(Of File)()
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
    QueueList.Add(New File(FileName, FileID))
End Sub
Public Sub Queue(ByVal FileObj As File)
    QueueList.Add(FileObj)
End Sub

Then, to loop over list:

然后,循环遍历列表:

For Each item As File In QueueList
    'Console.WriteLine(item.FileID & vbTab & item.FileName)
Next item

回答by asawyer

You need to switch to an object to hold your file information, and drop ArrayList for a strongly typed collection.

您需要切换到一个对象来保存您的文件信息,并为强类型集合删除 ArrayList。

public class QueueFile
    public Property FileID as integer
    public property FileName as string
end class

...

...

Private QueueList As New List(Of QueueFile)
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
    Dim QueueObj As New QueueFile
    QueueObj.FileID = "Test 1"
    QueueObj.FileName = "Test 2"
    QueueList.Add(QueueObj)
End Sub

回答by Jim Wooley

If you only have two values, you may find using a generic Dictionary even better than an ArrayList (requiring boxing and unboxing the types) or List(Of T) which retains type safety.

如果您只有两个值,您可能会发现使用通用 Dictionary 甚至比 ArrayList(需要装箱和拆箱类型)或保留类型安全的 List(Of T) 更好。

Private QueueList As New Dictionary(of Integer, String)
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
   QueueList.Add(FileID, FileName)
End Sub

If you really want a Queue as your method name indicates, consider using the generic Queue. Also, if you only need a key/value pair, you don't need to create your own class. You can use the generic KeyValuePair(Of T, S):

如果您真的想要一个 Queue 作为您的方法名称所指示的,请考虑使用通用 Queue。此外,如果您只需要一个键/值对,则不需要创建自己的类。您可以使用通用 KeyValuePair(Of T, S):

Private QueueItems As New Queue(Of KeyValuePair(Of Integer, String))
Public Sub Queue(ByVal FileName As String, ByVal FileID As Integer)
   QueueItems.Enqueue(New KeyValuePair(Of Integer, String)(FileID, FileName))
End Sub

To get items out, use the QueueItems.Dequeue.

要取出项目,请使用 QueueItems.Dequeue。