3

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!

1
  • 2
    Why you using arraylist?? Use list for this work. Commented Oct 11, 2011 at 13:57

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

With Option Strict Off in VB 10, you can just use Object and use the dynamic features added in .Net 4.
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.