Wish VB6 Collection like thingy


Well-Known Member
Licensed User
Lists and maps are very useful, but VB6 had a construct that was even better, the Collection.

Basically, it was like a combination of a map and a list, with all the features of both. Items could be retrieved by index or by key, it had an order, you could insert objects at any position and so on.

This is immensely useful in many situations, and the list and the map don't really measure up. The list doesn't have keys, the map doesn't have a reliable order and the ability to insert items at a specific position, and the lack of any means to combine these two blocks many elegant solutions.

For example, writing a cache manager using collections is trivial. Just look if the key is in the collection. If it is, retrieve data from the collection, then move it to the start of the collection. If not, fetch data from outside the cache, then add it to the start. If the cache is larger than it should be, discard the last item.

A similar problem where a collection would be useful is a playlist. I'm currently writing an implementation, where the GUI needs to get data from the playlist by key, yet the playlist needs to maintain and modify the order of the list.

Sure, it's possible to connect a map and a list with some code, but it's kind of fiddly and error prone, and it would be better to have it properly package in a Collection data type.


Well-Known Member
Licensed User
That's correct.

And that inhibits many very elegant solutions.

The order will be preserved if you use KeyValueStore class (or RandomAccessFile.WriteB4XObject).

Of course. I use a textwriter to write it line by line, also works nicely. It somehow negates the usefulness of File.WriteMap and File.ReadMap, though.

In other words, if I was to boil it all down to the bare necessities:

* Method to insert items at specific position in map.
* File.WriteMap and File.ReadMap should preserve order. For me, this is not strictly necessary, as I usually use strings (or stuff that can easily be converted to strings), so it's easy to make my own alternatives. For neatness, though, it should be done, as it's something that, if one is not aware of the limitation, can cause some very confusing bugs.