如何在VB.NET中创建自定义对象/自定义对象列表?

时间:2010-01-24 04:23:01

标签: vb.net multithreading list custom-object

我需要两个单独的列表,每个项目都是Integer,String,Bitmap - 每个项目都是Integer,String String。但是,我不知道如何做到这一点,甚至不知道在哪里 - 我已经搜索了自定义对象和自定义对象列表。我想要做的就是这个。 自定义Object1是Integer,String,Bitmap 自定义Object2是Integer,String,String

在一个线程中,我将向List1(Of Object1)添加项目,并处理它们,并将结果添加到List2(Of Object2),但是我需要能够从其他线程查看列表并说只给我Integer =(我的线程ID)的项目,这可能吗?任何帮助,甚至是与此请求相关的信息链接都会有帮助吗?

2 个答案:

答案 0 :(得分:3)

做这样的事情:

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)

当然,你会想要使用更有意义的名字。至于多线程方面,请考虑使用Monitor类在一个线程使用它时跨所有线程锁定列表。

答案 1 :(得分:0)

     Private Class Object1
        Public Property int() As Integer
            Get
                Return _int
            End Get
            Set(ByVal value As Integer)
                _int = value
            End Set
        End Property

        Public Property str() As String
            Get
                Return _str
            End Get
            Set(ByVal value As String)
                _str = value
            End Set
        End Property

        Public Property bmp() As Bitmap
            Get
                Return _bmp
            End Get
            Set(ByVal value As Bitmap)
                _bmp = value
            End Set
        End Property

        Friend _int As Integer
        Friend _str As String
        Friend _bmp As Bitmap

        Public Sub New(ByVal int As Integer, ByVal str As String, ByVal bmp as Bitmap)
            _int = int
            _str = str
            _bmp = bmp
        End Sub
    End Class 

然后你可以像这样初始化它......

Dim obj1 as List (Of Object1)
obj1.Add(New Object1(myInt, myStr, myBmp))