我有一个继承自CollectionBase的类。我尝试使用contains方法在插入新密钥之前检测Key是否已存在。这是我尝试过的。
<Serializable()> Public Class validationList
Inherits CollectionBase
Public Function Add(ByVal Item As validationItem) As Integer
Return Me.List.Add(Item)
End Function
Default Public ReadOnly Property Item(ByVal index As Integer) As validationItem
Get
Return CType(List.Item(index), validationItem)
End Get
End Property
Public Sub Remove(ByVal index As Integer)
Me.List.RemoveAt(index)
End Sub
Protected Overrides Sub OnInsert(ByVal index As Integer, ByVal value As Object)
If Me.List.Contains(value) Then MsgBox("Already exist")
MyBase.OnInsert(index, value)
End Sub
Public Function IndexOf(ByVal key As validationItem)
Return List.IndexOf(key)
End Function
Public Sub AddRange(ByVal item() As validationItem)
For counter As Integer = 0 To item.GetLength(0) - 1
List.Add(item(counter))
Next
End Sub
End Class
<Serializable()> Public Class validationItem
Implements IEquatable(Of validationItem)
Private _key As validationTypes
Private _value As String
Public Sub New()
' Empty constructor is needed for serialization
End Sub
Public Sub New(ByVal k As validationTypes, ByVal v As String)
_key = k
_value = v
End Sub
Public Enum validationTypes
Madatory = 0
[Integer] = 1
Numeric = 2
[Decimal] = 3
MaxValue = 4
MinValue = 5
MinLength = 6
Email = 7
End Enum
Public Property Value As String
Get
Return _value
End Get
Set(ByVal Value As String)
_value = Value
End Set
End Property
Public Property Key As validationTypes
Get
Return _key
End Get
Set(ByVal value As validationTypes)
_key = value
End Set
End Property
Protected Overloads Function Equals(ByVal eqItem As validationItem) As Boolean Implements IEquatable(Of Testing_Project.validationItem).Equals
If eqItem Is Nothing Then Return False
Return Me._key = eqItem.Key
End Function
Public Overrides Function Equals(ByVal eqItem As Object) As Boolean
If eqItem Is Nothing Then Return False
Dim eqItemObj As validationItem = TryCast(eqItem, validationItem)
If eqItemObj Is Nothing Then Return False
Return Equals(eqItemObj)
End Function
Public Overrides Function GetHashCode() As Integer
Return Me._key.GetHashCode()
End Function
End Class
validationList将作为属性从usercontrol公开,以便可以从设计器添加项目。添加项目时,我需要检测它们是否已存在。我尝试重写OnInsert,但有时返回重复项存在,即使它们不存在也不会报告当我尝试添加现有键时存在重复项。
答案 0 :(得分:0)
在处理有关Collection(Of T)
:
如果需要,添加对System.Collections.ObjectModel
的引用,然后
Imports System.Collections.ObjectModel
' a ValidationItem collection class
Public Class ValidationItems
Inherits Collection(Of ValidationItem)
Public Shadows Sub Add(NewItem As ValidationItem)
' test for existence
' do not add if it is not unique
Dim dupe As Boolean = False
For n As Int32 = 0 To Items.Count - 1
If Items(n).Key = NewItem.Key Then
dupe = True
Exit For
End If
Next
If dupe = False then
items.Add(newitem)
End if
' I would actually use an IndexOfKey function which might
' be useful elsewhere and only add if the return is -1
If IndexOfKey(NewItem.Key) <> -1 Then
Items.Add(newItem)
End If
End Sub
某些.NET集合类型将Add
实现为函数并返回添加的项。这听起来很奇怪,因为你传递了要添加的项目。但是,如果无法添加项目,则返回Nothing是一个整洁的信号量,因为&#34;我不能/不会这样做&#34;。我不记得std NET Collection Editor是否认识到了这一点。
使用Contains
的一个问题是它将测试item
是否传递,因为param与列表中的对象相同。它们永远不会是相同的对象,即使它们具有相同的值。在循环中测试密钥比调用实现接口的方法更简单。 (之前的答案在所呈现的上下文中完全有效,但背景已经改变)。
即使您使用CollectionBase,也希望在Add
中处理它。如果您尝试在OnInsert
中删除它,VS将无法反序列化集合。
此外,您的validationitem
需要一个Name属性,或者Collection Editor会显示&#34; Myassembly + MyType&#34;作为名称(或ToString覆盖)。
其他问题:
IndexOf
是否有效。该列表包含ValidationItems(对象),但您检查它是否为_key
(字符串)。如果您更改为为您实现它的Collection(Of T)
,则无关紧要。