Option Explicit
Sub test()
Dim mySL As Object
Set mySL = CreateObject("system.collections.sortedlist")
Dim myKey As Long ' double, string, date, variant, udt, ...
Dim i As Long
' add unsorted items. note: strong type checking
With mySL
myKey = 1000: .Add myKey, myKey ' long
myKey = 10: .Add myKey, "ten" ' string
myKey = 9: .Add myKey, 9 ' integer
myKey = 100: .Add myKey, myKey
myKey = 1: .Add myKey, myKey
myKey = 99: .Add myKey, myKey
End With
' check existance before insert
myKey = 98
If mySL.ContainsKey(myKey) Then Debug.Print myKey & " no show" Else mySL.Add myKey, myKey
If mySL.ContainsKey(myKey) Then Debug.Print myKey & " exists " Else mySL.Add myKey, myKey
' update by index
For i = 0 To mySL.Count - 1
If VarType(mySL.GetByIndex(i)) = vbLong Then
' mySL.GetByIndex(i) = mySL.GetByIndex(i) * 10 ' throws exception
myKey = mySL.GetKey(i)
mySL(myKey) = mySL(myKey) * 10
End If
Next i
' ' update by entry: no go?
' Dim objEntry As Object ' objEntry must be of type DictionaryEntry
' For Each objEntry In mySL
' If VarType(objEntry) = vbLong Then objEntry = objEntry * 10
' Next objEntry
' update by key
myKey = 98
mySL(myKey) = CLng(1000) ' convert int to lng
mySL(myKey + 1) = CLng(1000)
' list all
For i = 0 To mySL.Count - 1
Debug.Print i, mySL.GetKey(i), mySL.GetByIndex(i), VarType(mySL.GetKey(i)), VarType(mySL.GetByIndex(i))
Next i
' get first occurence of value
Debug.Print
i = mySL.IndexOfValue(CLng(1000))
Debug.Print i, mySL.GetKey(i), mySL.GetByIndex(i)
i = i + 1
Debug.Print i, mySL.GetKey(i), mySL.GetByIndex(i)
' remove all
For i = 0 To mySL.Count - 1
mySL.RemoveAt 0
Next i
Set mySL = Nothing
End Sub