Post by (X) on Dec 3, 2023 19:14:45 GMT 1
I was a bit stumped trying to change the value of a collection item...
Not only did I fail to realise that 'a = b' does not apply to collection items, but, I also had to figure out how to format the index variable I was trying to use in the collection statement as the item index.
You can eventually 'roll your own' functions that will simplify the task of changing the value of a collection item, but not before wondering why this is even necessary.
Anyway I hope this helps someone avoid a few perplexing hours of head scratching.
$Library "gfawinx"
$Library "UpdateRT"
UpdateRuntime ' Patches GfaWin23.Ocx
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Test Set collection item by Index
'
Dim C_Collection As New Collection
C_Collection.Add 1
C_Collection.Add 2
C_Collection.Add 3
Dim i As Long = 2
Trace C_Collection.Count
Trace C_Collection(i)
//
// Apparently you can not change the value of a collection item using the = sign.
// This is perhaps something that should be pointed out in the help documentation.
//
Dim v As Variant
Trace v
Debug
For Each v In C_Collection
Trace v
Next
v = C_Collection(i) + 10
'P_SetCollectionItem(C_Collection, i, v)
'P_SetCollectionItem2(C_Collection, i, v)
P_SetCollectionItem3(C_Collection, i, v)
Debug
For Each v In C_Collection
Trace v
Next
Proc P_SetCollectionItem(ByRef C_ As Collection, i%, v As Variant)
C_.Add v, After := Val(i)
C_.Remove Val(i)
EndProc
Proc P_SetCollectionItem2(ByRef C_ As Collection, i As Variant, v As Variant)
C_.Add v, After := i
C_.Remove CLong(i)
EndProc
Proc P_SetCollectionItem3(ByRef C_ As Collection, i, v As Variant)
C_.Add v, After := Val(i)
C_.Remove Val(i)
EndProc