Ho un oggetto di tipo HashTable. Memorizzo i dati e li voglio estrarre secondo l'ordine di inserimento.
Non riuscendo in maniera diretta, ho provato a fare una classe personalizzata, ereditata da DictionaryBase, che implementi IEnumerable.
Vi chiedo se potete controllare, dato che è il primo esempio del genere che faccio, ed è facile che abbia commesso errori grossolani.
codice:
Public Class HashtableInsertionOrder
Inherits DictionaryBase
Implements IEnumerable
Private _al As ArrayList
Public Sub New()
Me._al = New ArrayList()
End Sub
Public Sub Add(ByVal key As Object, ByVal value As Object)
If Not (Me.Dictionary.Contains(key)) Then
Me.Dictionary.Add(key, value)
Me._al.Add(key)
Else
Me.Dictionary("key") = value
End If
End Sub
Public Sub Remove(ByVal key As Object)
Me.Dictionary.Remove(key)
Me._al.Remove(key)
End Sub
Default Public Property Item(ByVal key As Object) As Object
Get
Return Me.Dictionary(key)
End Get
Set(ByVal value As Object)
Me.Dictionary(key) = value
End Set
End Property
Default Public Property Item(ByVal index As Integer) As DictionaryEntry
Get
Return New DictionaryEntry(Me._al(index), Me.Dictionary(Me._al(index)))
End Get
Set(ByVal value As DictionaryEntry)
Me.Dictionary(Me._al(index)) = value.Value
End Set
End Property
Public ReadOnly Property Keys() As Object()
Get
Return Me._al.ToArray()
End Get
End Property
Public ReadOnly Property Values() As Object()
Get
Dim o(Me._al.Count - 1) As Object
For i As Integer = 0 To Me._al.Count - 1
o(i) = Me.Dictionary(Me._al(i))
Next
Return o
End Get
End Property
Public Sub SortKeys()
Me._al.Sort()
End Sub
Public Sub SortValues()
Dim k As Object() = Me.Keys()
Dim v As Object() = Me.Values()
Array.Sort(v, k)
Me._al = New ArrayList(k)
End Sub
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return New Enumerator(Me)
End Function
Private Class Enumerator
Implements IEnumerator
Private _indice As Integer = -1
Private _M As HashtableInsertionOrder
Public Sub New(ByVal M As HashtableInsertionOrder)
Me._M = M
End Sub
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If Me._indice < (Me._M._al.Count - 1) Then
Me._indice += 1
Return True
Else
Return False
End If
End Function
Public ReadOnly Property Current() As Object Implements IEnumerator.Current
Get
Return Me._M.Item(Me._indice)
End Get
End Property
Public Sub Reset() Implements IEnumerator.Reset
End Sub
End Class
End Class
la collaudo così:
codice:
Dim ht As New HashtableInsertionOrder()
ht.Add("stefano", 10)
ht.Add("carlo", 20)
ht.Add("nicola", 30)
ht.Add("pietro", 40)
ht.Add("lucia", 50)
PrintLn("ordine di inserimento: ciclo each")
Response.Write("<hr />")
For Each de As DictionaryEntry In ht
PrintLn(de.Value, de.Key.ToString())
Next
PrintLn()
PrintLn("ordine di inserimento: ciclo for")
Response.Write("<hr />")
For i As Integer = 0 To ht.Count - 1
PrintLn(ht(i).Value, ht(i).Key.ToString)
Next
PrintLn()
PrintLn("ordine alfabetico delle chiavi")
Response.Write("<hr />")
ht.SortKeys()
'For i As Integer = 0 To ht.Count - 1
' PrintLn(ht(i).Value, ht(i).Key.ToString)
'Next
For Each de As DictionaryEntry In ht
PrintLn(de.Value, de.Key.ToString())
Next
PrintLn()
PrintLn("ordine alfabetico dei valori")
Response.Write("<hr />")
ht.SortValues()
'For i As Integer = 0 To ht.Count - 1
' PrintLn(ht(i).Value, ht(i).Key.ToString)
'Next
For Each de As DictionaryEntry In ht
PrintLn(de.Value, de.Key.ToString())
Next
Response.Write("<hr />")