与子集合的集合

时间:2016-09-07 11:35:52

标签: vb.net collections

假设我有一个帖子,其中一个帖子有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

只是要清楚:目标是组织收藏。每个帖子应该有他自己的帖子。

我在一个例子中选择了论坛,但它可能是其他任何东西.. 如果我不清楚,请帮助我..这不是我的母语(但不要担心 - 我可以阅读。;))

1 个答案:

答案 0 :(得分:0)

在我正确的项目中使用Dictionary。我只是想探索更多.. 无论如何,我尝试了你所说的“你必须使用Thread的集合,其中每个Thread实例都有Post的集合” 这就是结果:

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

现在我的问题是:这应该是这样的?或者有更好的方法来写这个?