OVH Cloud OVH Cloud

Hashtable ou SortedList restituant dans l'ordre d'intro

4 réponses
Avatar
François Müller
Bonjouir

Je cherche une classe dans le style SortedList ou Hashtable, qui, tout en
conservant l'acces par clef, puisse me restituer sur une enumeration les
entrees dans l'ordre de leur creation

Merci

Francois

4 réponses

Avatar
Ambassadeur Kosh
> Je cherche une classe dans le style SortedList ou Hashtable, qui, tout en
conservant l'acces par clef, puisse me restituer sur une enumeration les
entrees dans l'ordre de leur creation



fait la toi même en utilisant deux collections. une sorted, et une non
sorted.
suffit de fournir un IEnumerator dans ta classe englobante qui parcours la
bonne, et un accesseur par clef qui accede la deuxieme.

un template, et c'est torché en une demi heure jusqu'à la fin des temps
Avatar
Zazar
Bonsoir,

Je cherche une classe dans le style SortedList ou Hashtable, qui, tout en
conservant l'acces par clef, puisse me restituer sur une enumeration les
entrees dans l'ordre de leur creation



Si vous n'avez pas besoin de performances ou si vous avez peu d'éléments,
regardez du coté de la classe ListDictionnary.

--
Zazar
Avatar
François Müller
"Ambassadeur Kosh" a écrit dans le message de
news: %
fait la toi même en utilisant deux collections. une sorted, et une non
sorted.
suffit de fournir un IEnumerator dans ta classe englobante qui parcours la
bonne, et un accesseur par clef qui accede la deuxieme.



Merci. Mais peu habitue a .NET, je ne trouve pas la syntaxe pour implementer
l'interface IEnumerator (en VB)

Francois
Avatar
Ambassadeur Kosh
> Merci. Mais peu habitue a .NET, je ne trouve pas la syntaxe pour


implementer
l'interface IEnumerator (en VB)



ok, voici une collection fortement typée en VB. donne lui le nom qui te
convient
ensuite, ajoutes y la HashTable ou le Dictionary, en completent les methodes
de
la collection comme il se doit.

le ListDictionary n'est pas typé, mais en l'encapsulant comme il se doit
dans une
classe qui elle est bien typée, ça devrait le faire. et vu que tu sais
fabriquer des
Enumerateurs et comment on les place dans les collecions pour qu'ils
discutent
avec foreach, ça ne doit plus poser trop de soucis.

et chui deçu, j'ai pas trouvé de version générique du ListDictionary. snif.

allez, courage

--

Imports System
Imports System.Collections

Namespace DefaultNamespace

'<summary>
' <para>
' A collection that stores <see cref='.Dummy'/> objects.
' </para>
'</summary>
'<seealso cref='.TypedCollection1'/>
<Serializable()> _
Public Class TypedCollection1
Inherits CollectionBase

'<summary>
' <para>
' Initializes a new instance of <see cref='.TypedCollection1'/>.
' </para>
'</summary>
Public Sub New()
MyBase.New
End Sub

'<summary>
' <para>
' Initializes a new instance of <see cref='.TypedCollection1'/> based
on another <see cref='.TypedCollection1'/>.
' </para>
'</summary>
'<param name='value'>
' A <see cref='.TypedCollection1'/> from which the contents are
copied
'</param>
Public Sub New(ByVal value As TypedCollection1)
MyBase.New
Me.AddRange(value)
End Sub

'<summary>
' <para>
' Initializes a new instance of <see cref='.TypedCollection1'/>
containing any array of <see cref='.Dummy'/> objects.
' </para>
'</summary>
'<param name='value'>
' A array of <see cref='.Dummy'/> objects with which to intialize the
collection
'</param>
Public Sub New(ByVal value() As Dummy)
MyBase.New
Me.AddRange(value)
End Sub

'<summary>
'<para>Represents the entry at the specified index of the <see
cref='.Dummy'/>.</para>
'</summary>
'<param name='index'><para>The zero-based index of the entry to locate in
the collection.</para></param>
'<value>
' <para> The entry at the specified index of the collection.</para>
'</value>
'<exception cref='System.ArgumentOutOfRangeException'><paramref
name='index'/> is outside the valid range of indexes for the
collection.</exception>
Public Default Property Item(ByVal index As Integer) As Dummy
Get
Return CType(List(index),Dummy)
End Get
Set
List(index) = value
End Set
End Property

'<summary>
' <para>Adds a <see cref='.Dummy'/> with the specified value to the
' <see cref='.TypedCollection1'/> .</para>
'</summary>
'<param name='value'>The <see cref='.Dummy'/> to add.</param>
'<returns>
' <para>The index at which the new element was inserted.</para>
'</returns>
'<seealso cref='.TypedCollection1.AddRange'/>
Public Function Add(ByVal value As Dummy) As Integer
Return List.Add(value)
End Function

'<summary>
'<para>Copies the elements of an array to the end of the <see
cref='.TypedCollection1'/>.</para>
'</summary>
'<param name='value'>
' An array of type <see cref='.Dummy'/> containing the objects to add to
the collection.
'</param>
'<returns>
' <para>None.</para>
'</returns>
'<seealso cref='.TypedCollection1.Add'/>
Public Overloads Sub AddRange(ByVal value() As Dummy)
Dim i As Integer = 0
Do While (i < value.Length)
Me.Add(value(i))
i = (i + 1)
Loop
End Sub

'<summary>
' <para>
' Adds the contents of another <see cref='.TypedCollection1'/> to the
end of the collection.
' </para>
'</summary>
'<param name='value'>
' A <see cref='.TypedCollection1'/> containing the objects to add to the
collection.
'</param>
'<returns>
' <para>None.</para>
'</returns>
'<seealso cref='.TypedCollection1.Add'/>
Public Overloads Sub AddRange(ByVal value As TypedCollection1)
Dim i As Integer = 0
Do While (i < value.Count)
Me.Add(value(i))
i = (i + 1)
Loop
End Sub

'<summary>
'<para>Gets a value indicating whether the
' <see cref='.TypedCollection1'/> contains the specified <see
cref='.Dummy'/>.</para>
'</summary>
'<param name='value'>The <see cref='.Dummy'/> to locate.</param>
'<returns>
'<para><see langword='true'/> if the <see cref='.Dummy'/> is contained in
the collection;
' otherwise, <see langword='false'/>.</para>
'</returns>
'<seealso cref='.TypedCollection1.IndexOf'/>
Public Function Contains(ByVal value As Dummy) As Boolean
Return List.Contains(value)
End Function

'<summary>
'<para>Copies the <see cref='.TypedCollection1'/> values to a
one-dimensional <see cref='System.Array'/> instance at the
' specified index.</para>
'</summary>
'<param name='array'><para>The one-dimensional <see cref='System.Array'/>
that is the destination of the values copied from <see
cref='.TypedCollection1'/> .</para></param>
'<param name='index'>The index in <paramref name='array'/> where copying
begins.</param>
'<returns>
' <para>None.</para>
'</returns>
'<exception cref='System.ArgumentException'><para><paramref name='array'/>
is multidimensional.</para> <para>-or-</para> <para>The number of elements
in the <see cref='.TypedCollection1'/> is greater than the available space
between <paramref name='arrayIndex'/> and the end of <paramref
name='array'/>.</para></exception>
'<exception cref='System.ArgumentNullException'><paramref name='array'/>
is <see langword='null'/>. </exception>
'<exception cref='System.ArgumentOutOfRangeException'><paramref
name='arrayIndex'/> is less than <paramref name='array'/>'s lowbound.
</exception>
'<seealso cref='System.Array'/>
Public Sub CopyTo(ByVal array() As Dummy, ByVal index As Integer)
List.CopyTo(array, index)
End Sub

'<summary>
' <para>Returns the index of a <see cref='.Dummy'/> in
' the <see cref='.TypedCollection1'/> .</para>
'</summary>
'<param name='value'>The <see cref='.Dummy'/> to locate.</param>
'<returns>
'<para>The index of the <see cref='.Dummy'/> of <paramref name='value'/>
in the
'<see cref='.TypedCollection1'/>, if found; otherwise, -1.</para>
'</returns>
'<seealso cref='.TypedCollection1.Contains'/>
Public Function IndexOf(ByVal value As Dummy) As Integer
Return List.IndexOf(value)
End Function

'<summary>
'<para>Inserts a <see cref='.Dummy'/> into the <see
cref='.TypedCollection1'/> at the specified index.</para>
'</summary>
'<param name='index'>The zero-based index where <paramref name='value'/>
should be inserted.</param>
'<param name=' value'>The <see cref='.Dummy'/> to insert.</param>
'<returns><para>None.</para></returns>
'<seealso cref='.TypedCollection1.Add'/>
Public Sub Insert(ByVal index As Integer, ByVal value As Dummy)
List.Insert(index, value)
End Sub

'<summary>
' <para>Returns an enumerator that can iterate through
' the <see cref='.TypedCollection1'/> .</para>
'</summary>
'<returns><para>None.</para></returns>
'<seealso cref='System.Collections.IEnumerator'/>
Public Shadows Function GetEnumerator() As DummyEnumerator
Return New DummyEnumerator(Me)
End Function

'<summary>
' <para> Removes a specific <see cref='.Dummy'/> from the
' <see cref='.TypedCollection1'/> .</para>
'</summary>
'<param name='value'>The <see cref='.Dummy'/> to remove from the <see
cref='.TypedCollection1'/> .</param>
'<returns><para>None.</para></returns>
'<exception cref='System.ArgumentException'><paramref name='value'/> is
not found in the Collection. </exception>
Public Sub Remove(ByVal value As Dummy)
List.Remove(value)
End Sub

Public Class DummyEnumerator
Implements IEnumerator

Private baseEnumerator As IEnumerator

Private temp As IEnumerable

Public Sub New(ByVal mappings As TypedCollection1)
MyBase.New
Me.temp = CType(mappings,IEnumerable)
Me.baseEnumerator = temp.GetEnumerator
End Sub

Public ReadOnly Property Current As Dummy
Get
Return CType(baseEnumerator.Current,Dummy)
End Get
End Property

ReadOnly Property IEnumerator_Current As Object Implements
IEnumerator.Current
Get
Return baseEnumerator.Current
End Get
End Property

Public Function MoveNext() As Boolean
Return baseEnumerator.MoveNext
End Function

Function IEnumerator_MoveNext() As Boolean Implements
IEnumerator.MoveNext
Return baseEnumerator.MoveNext
End Function

Public Sub Reset()
baseEnumerator.Reset
End Sub

Sub IEnumerator_Reset() Implements IEnumerator.Reset
baseEnumerator.Reset
End Sub
End Class
End Class
End Namespace