字典以保证订单和索引获取

问题描述 投票:0回答:1

我想要未来的字典,它将以<key><value>的方式存储价值;保证订单意味着添加到列表中的每个新项目最后都作为下一个项目插入。我读到字典不是一个好选择因此我看了一下OrderedDictionary。此外,我希望也有可能通过索引(而不是键)获取项目。

我的主要问题是我是否选择了OrderedList作为我应该选择的那个?

然而,我正在努力通过索引获取物品。看下面了解问题:

Dim _finalList As New OrderedDictionary

_finalList.Add("John", "Miles")
_finalList.Add("Jessica", "Brown")

现在,我尝试过的以下任何一个都没有:

_finalList.Cast.ElementAt(0))
_finalList.Cast.ElementAt(0).Key.ToString())

_finalList.Cast(Of DictionaryEntry).ElementAt(0))
_finalList.Cast(Of DictionaryEntry).ElementAt(0).Key.ToString())

我得到的错误是:

no public Cast member for the Ordered Dictionary type.
vb.net
1个回答
0
投票

我建议你完成这个实现:

Public Class MyKeyedCollection(Of K, V)
    Implements IDictionary(Of K, V)

    Private lookup = New Dictionary(Of K, V)
    Private items = New List(Of KeyValuePair(Of K, V))

    Default Public Property Item(index As Integer) As V
        Get
            Throw New NotImplementedException()
        End Get
        Set(value As V)
            Throw New NotImplementedException()
        End Set
    End Property

    Default Public Property Item(key As K) As V Implements IDictionary(Of K, V).Item
        Get
            Throw New NotImplementedException()
        End Get
        Set(value As V)
            Throw New NotImplementedException()
        End Set
    End Property

    Public ReadOnly Property Keys As ICollection(Of K) Implements IDictionary(Of K, V).Keys
        Get
            Throw New NotImplementedException()
        End Get
    End Property

    Public ReadOnly Property Values As ICollection(Of V) Implements IDictionary(Of K, V).Values
        Get
            Throw New NotImplementedException()
        End Get
    End Property

    Public ReadOnly Property Count As Integer Implements ICollection(Of KeyValuePair(Of K, V)).Count
        Get
            Throw New NotImplementedException()
        End Get
    End Property

    Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).IsReadOnly
        Get
            Throw New NotImplementedException()
        End Get
    End Property

    Public Sub Add(key As K, value As V) Implements IDictionary(Of K, V).Add
        Throw New NotImplementedException()
    End Sub

    Public Sub Add(item As KeyValuePair(Of K, V)) Implements ICollection(Of KeyValuePair(Of K, V)).Add
        Throw New NotImplementedException()
    End Sub

    Public Sub Clear() Implements ICollection(Of KeyValuePair(Of K, V)).Clear
        Throw New NotImplementedException()
    End Sub

    Public Sub CopyTo(array() As KeyValuePair(Of K, V), arrayIndex As Integer) Implements ICollection(Of KeyValuePair(Of K, V)).CopyTo
        Throw New NotImplementedException()
    End Sub

    Public Function ContainsKey(key As K) As Boolean Implements IDictionary(Of K, V).ContainsKey
        Throw New NotImplementedException()
    End Function

    Public Function Remove(key As K) As Boolean Implements IDictionary(Of K, V).Remove
        Throw New NotImplementedException()
    End Function

    Public Function Remove(item As KeyValuePair(Of K, V)) As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).Remove
        Throw New NotImplementedException()
    End Function

    Public Function TryGetValue(key As K, ByRef value As V) As Boolean Implements IDictionary(Of K, V).TryGetValue
        Throw New NotImplementedException()
    End Function

    Public Function Contains(item As KeyValuePair(Of K, V)) As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).Contains
        Throw New NotImplementedException()
    End Function

    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of K, V)) Implements IEnumerable(Of KeyValuePair(Of K, V)).GetEnumerator
        Throw New NotImplementedException()
    End Function

    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Throw New NotImplementedException()
    End Function

End Class

您只需要在每个操作下保持lookupitems的完整性。

© www.soinside.com 2019 - 2024. All rights reserved.