Order of Dictionary Values

Is it safe to assume that Dictionary.Values will always return the values in the same order? I’m using a global property to hold the current index and I call Dictionary.Values at various times.

Thank you

are you using the dictionary from the old framework or the new framework?? in the new framework, it does NOT allow custom sort (always in alphabetical order). Say you have a list of Status - In Stock, Sold, Reserved, Consigned In and Consigned Out. If you want to have In Stock and Sold first and the rest alphabetical order, sorry, it will not work in the new framework

The array for values is only valid until the dictionary changes. Than a new array is built.

Changes in what way?

v = Dictionary.Values
Dictionary.Value( key) = Nil
v = Dictionary.Values <--------- Will the order of the values here be different than the order returned on Line 1 above?

Thank you

My understanding is that they could be. I think the only thing that’s guaranteed is, between modifications to the Dictionary, the order of Dictionary.Keys will match Dictionary.Values.

That stinks.

I have a dictionary property on the main window. I need to loop through this dictionary in the main thread.

When I come back into the main thread from another thread, I execute this:

// Clear out the dictionary item.
mD.Value( mCurrentItem.KeyID ) = Nil

// Clear out the current item object.
mCurrentItem = Nil

Dim v() as Variant

v = mD.values

If mCurrentIndex < v.Ubound then
// There’s more to do

mCurrentIndex = mCurrentIndex + 1

// Get the next dictionary item.
mCurrentItem = v( mCurrentIndex )


End if[/code]

Is there a better way?

Although in the old framework it seems they stay in order, the order of a dictionary is not guaranteed.
The .keys and .values methods return arrays that you can .sort.

The order is not guaranteed.

Setting an item to nil may not change it. But if the dictionary decides to reduce the number of bins because the number of entries got below a limit, the order can change.

what i did for my combobox in application is to assign use sql statement and loop through the record and assign the value to the dictionary for both the key and value. in my sql statement, it is sorted by custom sort like my example above. when i assign to the dictionary using the old framework, it appear exactly the same as the data in the sql statement. Using the new framework, dictionary appear as alphabetical order unless i have the value from the data is appended with a sequence say 01, 02, 03 follow with a - and then value from the data, then it will appear in custom order by then… when i selected the value in combobox, it will pick up 01-In Stock, 02-Sold and i need to split them out.