2016-09-07 171 views
0

讓我們假設我有一個論壇與3個職位的一個線程。集合與子集合

我想這個最終結果:

Dim MyFourm As new Fourm 
MyFourm.Thread.Add(545)''for ex 545 mean Thread ID. 

MyForum.Thread(0).Post(1).Username 

主題應該是整數(=線程ID)的集合 帖子應該是郵政型

所以集合在這種情況下,代碼說

Public Class Fourm 
'Thread should be inside this class and do some background code 
End Class 

Public Class Post 
Public Property Username as string 
Public Property PostContent as string 
End Class 

像「是誰寫這個帖子選擇了第一個線程,第二崗位和檢索用戶名」只是要一個明確的:我們的目標是奧爾加收集集合。每個主題應該有他自己的帖子。

我選擇論壇的例子,但它可以是其他任何東西.. 如果我不清楚,請幫助我..這不是我的母語(但不要擔心 - 我可以閱讀。))

+0

如果'Thread'是一個帶有整數的集合,它不能包含帖子。您必須使用'Thread'集合,其中每個'Thread'實例具有'Post'集合。另一種方法是使用「Dictionary(Of Int32,List(Of Post))」。所以關鍵是threadid,值是所有帖子的列表。 –

回答

0

在我正確的項目中使用字典iam。我只是想探討一些更.. 不管怎樣,我想你說的關於「你必須使用線程的集合,其中每個線程實例有郵政集」 ,這是結果:

Public Class MainFrm 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     Dim MyForum As New Forum 
     MyForum.Thread.Add(500)' some id's 
     MyForum.Thread.Add(120) 

     MyForum.Thread(0).Posts.Add(New ForumPost() With {.PostContent = "Therad ID: 500 | Post: 1#", .Username = "Don"}) 
     MyForum.Thread(0).Posts.Add(New ForumPost() With {.PostContent = "Therad ID: 500 | Post: 2#", .Username = "Shon"}) 
     MyForum.Thread(0).Posts.Add(New ForumPost() With {.PostContent = "Therad ID: 500 | Post: 3#", .Username = "Ron"}) 

     MyForum.Thread(1).Posts.Add(New ForumPost() With {.PostContent = "Therad ID: 120 | Post 1#", .Username = "Emi"}) 


     For iThread = 0 To MyForum.Thread.Count - 1 
      For iPost = 0 To MyForum.Thread(iThread).Posts.Count - 1 
       Static Pst As New ForumPost 
       Pst = MyForum.Thread(iThread).Posts(iPost) 
       Console.WriteLine($"Content:{Pst.PostContent}, Username who post it:{Pst.Username}") 
      Next 
     Next 
    End Sub 
End Class 
Public Class Forum 
    Public Property Thread As New ThreadCollection 

End Class 

Public Class ForumThread 
    Inherits List(Of Integer) 
    Public Property Posts As New PostCollection 
    Sub New(id As Integer) 

    End Sub 
End Class 

Public Class ThreadCollection 
    Inherits List(Of ForumThread) 
    Public Overloads Sub Add(ByVal id As Integer) 
     MyBase.Add(New ForumThread(id)) 
    End Sub 

End Class 

Public Class ForumPost 
    Public Property Username As String 
    Public Property PostContent As String 
End Class 

Public Class PostCollection 
    Inherits List(Of ForumPost) 
End Class 



' Content:Therad ID: 500 | Post: 1#, Username who post it:Don 
' Content:Therad ID: 500 | Post: 2#, Username who post it:Shon 
' Content:Therad ID: 500 | Post: 3#, Username who post it:Ron 
' Content:Therad ID: 120 | Post 1#, Username who post it:Emi 

現在我的問題是:這應該是這樣的?或者有更好的方法來寫這個?

+0

請幫忙嗎? – Yoal223