在VB6中,曾经有一种Collection数据类型,它允许通过其键或其序号检索集合中的项目。但是,它没有强类型。通用集合
现在,用VB.Net,我正在寻找一个合适的替代品,它是强类型的,可以与泛型集合一起使用。
这是我想要做的一个简单例子。唯一的问题是底层集合类BindingList不支持通过一个字母键高效地检索一个项目,所以我必须遍历元素来获取我正在寻找的项目。对于大型收藏,这不是有效的。
我已经查看了各种集合类型类,但没有找到合适的替代品。
这就是我想要做的,除了用Item属性完成的循环。
并非只是说“使用哈希表”或类似的东西,如果可以的话,请包括详细信息,正如我为下面的简短示例所做的那样。
Public Class Car
Public Sub New(ByVal keyName As String, ByVal property1 As String)
_KeyName = keyName
_Property1 = property1
End Sub
Dim _KeyName As String
Public Property KeyName() As String
Get
Return _KeyName
End Get
Set(ByVal value As String)
_KeyName = value
End Set
End Property
Public _Property1 As String
Public Property Property1() As String
Get
Return _Property1
End Get
Set(ByVal value As String)
_Property1 = value
End Set
End Property
End Class
Public Class Cars
Inherits System.ComponentModel.BindingList(Of Car)
Public Overloads ReadOnly Property Item(ByVal key As String) As Car
Get
For Each CurrentCar As Car In Me.Items
If CurrentCar.KeyName = key Then
Return CurrentCar
End If
Next
Return Nothing
End Get
End Property
End Class
是的,它看起来像字典对象会做得很好。我想我没有意识到它内部的元素可以键入 – ChadD 2009-05-02 21:19:37