How to create custom objects/list of custom objects in VB.NET?

fMinkel picture fMinkel · Jan 24, 2010 · Viewed 20.4k times · Source

I need two seperate lists, which every item is Integer, String, Bitmap - and one which every item is Integer, String String. However I don't know how to do this, or even where to look - I've googled for custom objects and custom object lists. What I'm trying to do is this. Custom Object1 is Integer, String, Bitmap Custom Object2 is Integer, String, String

In one thread I'll be adding items to List1(Of Object1), and processing them, and adding the results to List2(Of Object2), however I need to be able from other threads to look at the list and say only give me the items where Integer = (my thread ID), is this possible? Any help, or even links to information that would be relevant to this request would be helpful?

Answer

Joel Coehoorn picture Joel Coehoorn · Jan 24, 2010

Do something like this:

Public Class Type1
    Private _ThreadID As Integer
    Public Property ThreadID() As Integer
       Get
           Return _ThreadID
       End Get
       Set
           _ThreadID = Value
       End Set
    End Property

    Private _MyString As String
    Public Property MyString() as String
        Get
            Return _MyString
        End Get
        Set 
            _MyString = Value
        End Set
    End Property

    Private _MyBitmap As Bitmap
    Public Property MyBitmap As Bitmap
        Get
            Return _MyBitmap
        End Get
        Set
            _MyBitmap = Value
        End Set
    End Property
 End Class

.

Dim list1 As New List(Of Type1)()
''#  ... Add some items to the list...

''# List items with a given thread id:
Dim SomeThreadID As Integer = GetMyThreadID()
list1.Where(Function(o) o.ThreadID = SomeThreadID)

Of course, you'll want to use more meaningful names. As for the multi-threading aspect, look into using the Monitor class to lock your lists across all threads while one thread is using it.