每次我使用诸如Artikel之类的类时,如下所示:
Public Class Artikel
Property ID As Integer
Property Nummer As String
Property Name As String
Property Position As Integer
End Class
对于此类课程,我希望有收集课程。我想拥有的功能如下:
--> Add (passing Artikel object)
--> Remove (passing Artikel object)
--> Sort entire collection (based on Position property desc/asc)
--> Compare two Artikels (pass by Artikels and tell by which property has to be compared)
--> Check whether two artikels equals
--> Every added artikel has to be marked by Key (so maybe dictionary)? <key><Artikel>
--> Remove Artikel (passing by Key index)
您能在那里告诉我,甚至更好地提供收集类通过这些要求的例子吗?
编辑:启动:
Artikel的收藏:
Option Strict On
Public Class Articles
Public Property collection As Dictionary(Of Integer, Artikel)
Sub New()
'Initiate new collection
collection = New Dictionary(Of Integer, Artikel)
End Sub
'Add new Artikel to collection
Public Function AddToCollection(ByVal artikel As Artikel) As Boolean
collection.Add(artikel)
Return True
End Function
'Remove specific Artikel
Public Sub RemoveFromCollectionByArtikel(artikel As Artikel)
If Not IsNothing(collection) Then
collection.Remove(artikel)
End If
End Sub
'Get collection
Public Function GetCollection() As Dictionary(Of Integer, Artikel)
Return collection
End Function
'Sort collection by property position
Public Sub SortByPosition()
collection.Sort()
End Sub
'Remove specific sending keys and then reorder them
Public Sub RemoveAllMarkedAsDeleted(keys As List(Of Integer))
'-- Check whther anything has been marked as deleted
If keys.Count > 0 Then
For Each row In keys
collection.Remove(row)
Next
ReorderKeys()
End If
'Reorder all Artikels in collection
Private Sub ReorderKeys()
Dim newCollection As New Dictionary(Of Integer, Artikel)
Dim index As Integer = 0
For Each collitem In collection
newCollection.Add(index, collitem.Value)
index += 1
Next
collection.Clear()
collection = newCollection
End Sub
End Class
Artikel类(另外,我实现了IComparable以便进行排序)
Option Strict On
Public Class Artikel
Implements IComparable(Of Artikel)
Property ID As Integer
Property Nummer As String
Property Name As String
Property Position As Integer
Public Function CompareTo(pother As Artikel) As Integer Implements IComparable(Of Artikel).CompareTo 'we can sort because of this
Return String.Compare(Me.Position, pother.Position)
End Function
Public Shared Function FindPredicate(ByVal partikel As Artikel) As Predicate(Of Artikel)
Return Function(partikel2 As Artikel) partikel.ID = partikel2.ID
End Function
Public Shared Function FindPredicateByUserId(ByVal partikel As String) As Predicate(Of Artikel)
Return Function(partikel2 As Artikel) partikel = partikel2.ID
End Function
End Class
它的某些部分看起来不错,但是我最终会做一些不同的事情。首先,请考虑item类上的重载,以使其更易于创建和默认初始化:
Public Class Article
Property ID As Integer = -1
Property Key As String = ""
Property Name As String = ""
Property Position As Integer = -1
Property PubDate As DateTime = DateTime.Minimum
Public Sub New()
End Sub
' whatever minimum data a new item requires
Public Sub New(k As String, n As String)
Key = k
Name = n
End Sub
' full initialization:
Public Sub New(k As String, n As String, pos As Int32,
pubDt As DateTime)
...
End Sub
End Class
我添加了一些用于variant的属性,并且我怀疑“ Nummer”可能是OP中提到的“ Key”,但是无论它是什么,如果有一定的重要性,我都会将其作为该名称添加到Article
类中。
您可能需要一个简单的ctor进行序列化(???)。其中一些将查找并使用无Private
参数的构造函数,但是您的代码将被迫使用重载之一,以便在创建新重载时提供一些最低级别的数据。
您可能不需要IComparable
。通常用于更复杂的比较,例如多个或复杂的属性。一个例子是纸箱或盒子:
If (width = Other.Width) AndAlso (height = Other.Height) Then
Return 0
ElseIf (width = Other.Height) AndAlso (height = Other.Width) Then
Return 0
End If
再加上更多的回转,可以计算出比其他回转“少”的回转。您不需要它的一个原因是,它If Art1.Postion > Art2.Postion
是微不足道的。您遇到的另一个原因是,aDictionary
无法排序。
Dictionary
内部List而不是a ,可以更好地描述您所描述的某些内容,但仍然可以让您在需要的地方表现得像a一样Dictionary
。为此,我可以使用ICollection<T>
以下命令构建它:
Public Class ArticleCollection
Implements ICollection(Of Article)
在该行之后按Enter键将添加所有必需的方法,包括:
Public Sub Add(item As Article) Implements ICollection(Of Article).Add
Public Sub Clear() Implements ICollection(Of Article).Clear
Public Function Contains(item As Article) As Boolean Implements ICollection(Of Article).Contains
Public ReadOnly Property Count As Integer Implements ICollection(Of Article).Count
Public Function Remove(item As Article) As Boolean Implements ICollection(Of Article).Remove
如何实现这些完全取决于您。它还不排除添加诸如RemoveAt(int32)
或RemoveByKey(string)
取决于您需要什么/将如何使用它的方法。好处之一ICollection(Of T)
是它包含了IEnumerable
该功能,该功能将允许在每个循环中使用(一旦编写了Enumerator):For Each art In Articles
要模拟字典以仅允许一个具有特定属性值的项目,请执行以下操作:
Public Class ArticleCollection
Implements ICollection(Of Article)
Private mcol As List(Of Article)
...
Public Sub Add(item As Article) Implements ICollection(Of Article).Add
' check for existing key
If KeyExists(item.Key) = False Then
mcol.Add(item)
End If
End Sub
您还可以重载它们:
' overload to match Article ctor overload
Public Sub Add(key As String, name As String)
If KeyExists(key) = False Then
' let collection create the new item
' with the minimum required info
mcol.Add(New Article(key, name))
End If
End Sub
如果添加Item
属性,则可以索引集合(Articles(3)
):
Property Item(ndx As Int32) As Article
Get
If ndx > 0 AndAlso ndx < mcol.Count Then
Return mcol(ndx)
Else
Return Nothing
End If
End Get
Set(value As Article)
If ndx > 0 AndAlso ndx < mcol.Count Then
mcol(ndx) = value
End If
End Set
End Property
' overload for item by key:
Public Property Item(key As String) As Article
的Add
方法和Item
属性将是重要的,如果集合将在标准NET显示CollectionEditor
。
有几种实现排序的方法。最简单的方法是在使用集合的代码中使用linq:
Articles = New ArticleCollection
' add Article items
Dim ArticlesByDate = Articles.OrderBy(Function(s) s.PubDate).ToList()
我添加PubDate
的Article
属性之一在哪里。处理排序的另一种方式是通过collection类返回一个新的collection(但这很简单,几乎不需要):
Friend Function GetSortedList(bSortAsc As Boolean) As List(Of Article)
If bSortAsc Then
Return mcol.OrderBy(Function(q) q.PubDate).
ThenBy(Function(j) j.Position).ToList()
Else
Return mcol.OrderByDescending(Function(q) q.PubDate).
ThenByDescending(Function(j) j.Position).ToList()
End If
End Function
它是实现ICollection(Of T)
,继承ICollection(Of T)
还是起作用Dictionary
完全取决于它的含义,使用方式以及存在的任何规则和限制(包括是否要序列化以及如何进行序列化)。这些不是我们所知道的。
MSDN上有一篇关于“收集指南”的文章,非常出色。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句