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.
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!
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
You need to switch to an object to hold your file information, and drop ArrayList for a strongly typed collection.
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
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.
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):
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.