假设我有一个帖子,其中一个帖子有3个帖子。
我想得到最终结果:
Dim MyFourm As new Fourm
MyFourm.Thread.Add(545)''for ex 545 mean Thread ID.
MyForum.Thread(0).Post(1).Username
线程应该是整数的集合(=线程ID) 帖子应该是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
它的工作正常,但我的问题是否写得好?或者它可能以某种方式改善它?
答案 0 :(得分:0)
您的类型应如下所示:
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
这是整个.NET Framework中使用的模式,例如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
如果您想要自定义功能,那么您应该继承System.Collections.ObjectModel.Collection(Of T)
类而不是List(Of T)
,然后声明该类型的属性,例如。
Public Class Thread
Public ReadOnly Property Id As Integer
Public ReadOnly Property Posts As List(Of Post) = New List(Of Post)
Public Sub New(id As Integer)
Me.Id = id
End Sub
'Other members
End Class
Public Class ThreadCollection
Inherits Collection(Of Thread)
Public Sub AddNew(id As Integer)
Me.Add(New Thread(id))
End Sub
End Class
Public Class Forum
Public ReadOnly Property Threads As ThreadCollection = New ThreadCollection
'Other members
End Class