Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116

    [vb.net]interfacce IEnumerator e IEnumerable

    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 />")
    Pietro

  2. #2
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,472
    Cosa intendi per "non sono riuscito ad estrarre i valori dalla Hashtable in maniera diretta"?

    Mostra il codice e descrivi l'errore che hai ottenuto, perché penso sia un'operazione fattibile, e magari si tratta solo di una banale correzione da apportare al codice iniziale che hai scritto, senza dover necessariamente creare la classe che hai postato.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da alka
    Cosa intendi per "non sono riuscito ad estrarre i valori dalla Hashtable in maniera diretta"?

    Mostra il codice e descrivi l'errore che hai ottenuto, perché penso sia un'operazione fattibile, e magari si tratta solo di una banale correzione da apportare al codice iniziale che hai scritto, senza dover necessariamente creare la classe che hai postato.
    Dico che non so estrarre da un HashTable gli elementi inseriti secondo l'ordine di inserimento col classico for each.


    ps. anche se si potesse, la cosa che mi interessa di più è implementare le interfacce, in questo caso IEnumerable
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.