2016-09-09 13 views
0

私は3つの投稿と1つのスレッドを持つフォーラムを持っていると仮定します。サブコレクション付きのコレクション

私はこの最終的な結果を取得したい:私は辞書

を使用したくない*

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

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

スレッドは、整数の集合(=スレッドID)でなければなりません ポストはポストタイプ の集まりでなければなりませんこの場合、コードは「最初のスレッドを選択し、2番目の投稿とこの投稿を書くユーザーのユーザー名を取得する」のようなものです。

少しプレイした後、私はこのコードを記述します。その書き込み場合

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

あなたのタイプは、次のようになります。例えば、.NET Frameworkの、全体で使用されるパターンだ

Public Class Post 

    Public Property Username As String 

    'Other members 

End Class 

Public Class Thread 

    Private _posts As New List(Of Post) 

    Public ReadOnly Property Posts As List(Of Post) 
     Get 
      Return _posts 
     End Get 
    End Property 

    'Other members 

End Class 

Public Class Forum 

    Private _threads As New List(Of Thread) 

    Public ReadOnly Property Threads As List(Of Thread) 
     Get 
      Return _threads 
     End Get 
    End Property 

    'Other members 

End Class 

myDataSet.Tables(0).Rows(1).Item(2)。例えば

Public Class Thread 

    Public ReadOnly Property Posts As List(Of Post) = New List(Of Post) 

    'Other members 

End Class 

Public Class Forum 

    Public ReadOnly Property Threads As List(Of Thread) = New List(Of Thread) 

    'Other members 

End Class 

カスタム機能が必要な場合は、あなたではなくList(Of T)よりSystem.Collections.ObjectModel.Collection(Of T)クラスを継承し、その種類のあなたの特性を宣言する必要があり、:、これらの最後の2つのクラスがこれに折りたたむことができるVB 2015では

+0

ありがとうございます。私が探しているもの。 – Yoal223

+0

この回答があなたの問題を解決した場合は、それを受け入れるようにしてください。 – jmcilhinney

関連する問題