Private K As String, Dico As New Collection, Ks() As String
Function Exist(Key As String) As Boolean
Exist = InStr("©" & K & "©", "©" & Key & "©")
End Function
Property Get Intem(ByVal Key As String) As Variant
If Not Exist(Key) Then
If IsNumeric(Key) Then
Dim Ks() As String: Ks = Split(K, "©")
If UBound(Ks) < Val(Key) Then Dico.Add New ClasseValue, Key: K = K & "©" & Key Else Key = Ks(Val(Key))
Else
Dico.Add New ClasseValue, Key: K = K & "©" & Key
End If
End If
If IsObject(Dico(Key).Value) Then
Set Intem = Dico(Key).Value
Else
Intem = Dico(Key).Value
End If
End Property
Property Let Intem(ByVal Key As String, Value As Variant)
If Not Exist(Key) Then
If IsNumeric(Key) Then
Dim Ks() As String: Ks = Split(K, "©")
If UBound(Ks) < Val(Key) Then Dico.Add New ClasseValue, Key: K = K & "©" & Key Else Key = Ks(Val(Key))
Else
Dico.Add New ClasseValue, Key: K = K & "©" & Key
End If
End If
If IsObject(Dico(Key).Value) Then
Set Dico(Key).Value = Value
Else
Dico(Key).Value = Value
End If
End Property
Property Get Intems() As Variant
Dim T() As String, i As Integer, d As New Collection
Ks() = Split(K, "©")
For i = 1 To UBound(Ks)
d.Add Dico(Ks(i)), Ks(i)
Next
Set Intems = d
End Property
Public Sub Add(Value As Variant, Key As Variant)
If Key = "" Then
Dico.Add New ClasseValue
Else
If Exist(CStr(Key)) Then Err.Raise 457, "Dictionary", "Cette clé est déjà associée à un élément de cette collection"
Dico.Add New ClasseValue, CStr(Key)
K = K & "©" & Key
End If
If IsObject(Value) Then
Set Dico(CStr(Key)).Value = Value
Else
Dico(CStr(Key)).Value = Value
End If
End Sub
Public Sub Remove(Key As String)
Dico.Remove Key
K = Replace(K & "©", "©" & Key & "©", "©")
If Right(K, 1) = "©" Then Mid(K, Len(K), 1) = " "
K = Trim(K)
End Sub
Public Sub Clear()
Set Dicoc = New Collection: K = ""
End Sub
Public Sub Sort(Optional Desc As Boolean = True)
Dim T() As String
T() = Split(K, "©")
For i = 2 To UBound(T)
If T(i - Abs(Desc)) > T(i - Abs(Not Desc)) Then
a = T(i - Abs(Desc))
T(i - Abs(Desc)) = T(i - Abs(Not Desc))
T(i - Abs(Not Desc)) = a
i = i - 2
If i < 1 Then i = 1
End If
Next
K = Join(T, "©")
End Sub
Public Function Keys() As String()
Dim T() As String, Ks() As String, i As Integer
T() = Split(K, "©")
ReDim Ks(1 To UBound(T))
For i = LBound(Ks) To UBound(Ks)
Ks(i) = T(i)
Next
Keys = Ks
End Function