Implement iterator on Collection?

I have a class ‘DataSet’ whose super is ‘Collection’
When it try to use it in a for each loop i get a message that says I haven’t implemented the iterator class. Can someone show me an example of how to implement an iterator over a Collection via the interface?

for each el as element in myDataSet
...
next

This object does not implement the Iterable interface and cannot be used in a For Each loop

A collection is a datatype that is obsolete and should have been removed ages ago.

Why are you using a collection?

Because the code was written ages ago. lol.

The code must be Murican drinking age by now. Can you use a dictionary instead?

1 Like

IIRC, Collections guarantee the order of the items won’t change while Dictionary does not. That’s not to say that they won’t be, just that it’s not guaranteed.

Now… you could subclass Dictionary to add an index to be sure that you can always get things back in the same order as they were added, but that would surely slow things down, especially when adding or removing values.

1 Like

I have used Dictionary in many projects in the last 10 years and never happened to get the order shuffled using the for…each construct.
I also had this discussion with many other developers and everyone agreed on the fact it’s safe to use in this way.