2016-09-09 3 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 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 프레임 워크에서 사용되는 패턴입니다

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).

은 VB 2015 년, 그 마지막 두 클래스는이 붕괴 될 수있다 : 사용자 정의 기능을 원하는 경우

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) 클래스를 상속하고 해당 유형, 예를 들어, 당신의 특성을 선언해야

+0

감사합니다. 그게 내가 원하는거야. – Yoal223

+0

이 답변으로 문제가 해결되면 수락하십시오. – jmcilhinney

관련 문제