2010-01-24 182 views
2

我需要兩個單獨的列表,每個項目是整數,字符串,位圖 - 每個項目是整數,字符串字符串。然而,我不知道該怎麼做,甚至不知道怎麼看 - 我已經搜索了自定義對象和自定義對象列表。我想要做的是這個。 定製Object1是整數,字符串,位圖 自定義對象2是整數,字符串,字符串如何在VB.NET中創建自定義對象/自定義對象列表?

在一個線程我會增加項目列表1(中Object1),並處理它們,並把結果加入列表2(共Object2),但是我需要能夠從其他線程看看列表,並說只給我的整數=(我的線程ID)的項目,這可能嗎?任何幫助,甚至鏈接到與此請求相關的信息都會有幫助嗎?

回答

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類在一個線程正在使用它的情況下鎖定所有線程的列表。

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)) 
+0

您已將所有公開屬性聲明爲日期 – AUSteve

+0

良好的捕獲...不幸的是複製/粘貼並不總是你的朋友。 –