Collections: Why not?

I saw this in a different post:

[quote]https://forum.xojo.com/8852-collections

To me, the only use case for Collections is when dealing with JSONItems: you can build up a nested structure of Dictionaries containing Collections in their keys and convert that to JSON Objects with embedded Arrays.[/quote]

The docs say:

[quote]
The concept of a collection is similar to that of an array, except that each element in a collection can be a different data type.[/quote]
and

This doesnt appear to be true.
I have several collections in my app, and hearing of it being in some way deprecated, I set off today to replace my Collections with Dictionaries, expecting it to be a simple case of changing Collection to ‘Dictionary’

I cannot.

I have been using this kind of code :

c= colB.count if c > 0 then for x = c downto 1 theObject = colB.item(x) if theobject.has_a_particular_attribute then colB.remove(x) end if next end if

So I cant test the object, and delete it by ordinal value

With a Dictionary, I cannot iterate by ordinal value…
I can either iterate through the keys, or iterate through the values
If working through the values, I cannot delete the item Im looking at because Remove wants the key , and I don’t have that.

Ami I missing something? Dictionary does NOT offer all the functionality of a Collection…?

With an old dictionary, you can loop through all keys, get their value and test that against your condition. If it matches, delete the key:

For each Key as Variant in myDictionary.Keys If myDictionary.value(key) = myCondition then myDictionary.Remove key Next

A Xojo.Core.Dictionary is iterable, you can loop

For each Entry as DictionaryEntry in myDictionary If entry.value = myCondition Then myDictionary.Remove Entry.Key Next

I’ll give that a try.

I expected that removing an item half way through the iteration would disrupt the iteration somehow?
eg if you have 10 items, and delete the 4th and 5th, the 9th and 10th have ‘moved’ by the time you get there.
Maybe Im too focussed on how an array works.

If that should happen, buffer the keys in an array and remove them after the loop. (I did not test the code)

[quote=335398:@Jeff Tullin]
This doesnt appear to be true.[/quote]
It is but its not a line for line replacement of Collection with Dictionary

 c= colB.count
    if c > 0 then
      for x = c  downto  1
          dim theKey as variant = colB.Key(x) // get the key
           theObject = colB.value(x) // now get the value for that key
           if theobject.has_a_particular_attribute then
             colB.remove(x)
           end if
       next
   end if

Thanks for the input Norm and Ulrich

I spent some time today switching over, but reverted the code.
Finding an existing item by key is faster in a Dictionary
Iterating through all records should be fast if I don’t want to delete any…

for each x in dict.values

But the multi step approach just adds a little extra code and variables.
No worries… just don’t actually deprecate Collection please. It has its uses.