Storage question

Suppose I have a listbox, and I load a set of rows therein. The user will click on some of these rows, which are then highlighted. Later, rows in the listbox are completely replaced by another set, and the user may click on some of its rows too. I may have several such sets of rows, and I switch back and forth between the sets of rows, but want to show the appropriate set of clicked rows as highlighted again. So, I must remember which rows have been clicked in each set.

I can use a dictionary to store the information, with an id for each set of rows. Then the rowset for that id will be an array of rows. So, in the listbox cellClick handler, I might do something like:

[code]Dim rows() as integer

rows = selectDict.value(rowsetID)
rows.Append (row)
selectDict.Value(rowsetID) = rows
[/code]

(ignoring matters such as clicking the same row more than once, and where I get rowsetID from :slight_smile:

Now, first of all, once I’ve appended the row, do I actually need to update the dictionary entry? I had a feeling this was not necessary.

Second, after the handler has finished, where is the storage for rows() ? And is it freed if I replace that dictionary entry or delete it? Do I have to do any tidying up or is all that space properly garbage collected?

When you assign an array to anything (property, variable, Dictionary entry, etc.), you are assigning a reference to that array, much like when you assign an object. Since there is only one array, there is no benefit or need to assign it again after changing it. Once all the references to the array are gone, the array will be gone so there is nothing more for you to do.

Having said that, in your particular use case, consider using a Dictionary to track the rows instead of an array. A Dictionary entry that holds another Dictionary is perfectly valid, and a Dictionary key does not have to have a value, e.g., …

dict.Value( row ) = nil

If you use a Dictionary, you will not have to worry about assigning the same row multiple times, so adding a row becomes easier. Removing a row is about the same amount of work, albeit different code.

Thanks. Oddly enough, I had been considering moving from an array to a dictionary. One annoyance about a listbox is that if I delete a row, I’ll have to rebuild the data structure as the row numbers may change. I’m converting this from JavaScript, and the equivalent structure is a set of pointers to HTML table rows, which don’t change if I delete one (the upside, however, is that I get sorting and changing column widths for next to no cost).

How about don’t populate the listbox AT all… If you have a series of “pointers”,
just put empty rows in the listbox, and put the pointer in ROWTAG

Then in the CellTextPaint and/or CellBackgroundPaint event, take the row tag, look up the data in your external source, and draw/paint it accordingly.