Remove Match from array of Dictionaries

I am having difficulty removing an individual dictionary from an array of dictionaries.

I have an array in memory of a unique id called ‘uuid’ and an iosimage called ‘picture’

I do this so I can capture pictures rapidly into memory and then process them.

The processing is where I am having difficulty. I just want to remove the individual dictionary as I process it.

uuid coming in is from a database record that I want to add the picture to. So it matches to a specific database record that also has the same unique id. So once I have saved the picture to its corresponding database record. I want to remove it from the App.PictureArray

Here is how I am processing through the array:

[code]
Dim IntCount as Integer=0
For Each entry as Dictionary in App.PictureArray

Dim PictureDict As Dictionary = entry.Clone

If uuid= PictureDict.Value(“uuid”) Then
picture=PictureDict.Value(“picture”)
'Have tried but did not work
App.PictureArray.Remove(intCount)
End If
intCount=intCount+1
Next
[/code]

Any ideas on a better way to do this?

So first, you shouldn’t use a for each loop with a index counter as you are here.
Per the language ref: “A For…Each loop does not guarantee that it will loop through the values in the array in index order. Do not make any assumptions of the traversal order as it is subject to change in the future.”

So, use a traditional for loop, but be sure to loop from ubound downto 0 in order to avoid out of bounds exceptions after you remove elements from the array.

So something more like:

for i as integer = App.PictureArray.Ubound downto 0 if uuid = App.PictureArray(i).Value("uuid") then picture = App.PictureArray(i).Value("picture") App.PictureArray.Remove(i) end next

The array is of Dictionaries containing the values uuid and picture. I don’t believe I can access them just as values of App.PictureArray(i)?

[quote=387962:@Jared Feder]So first, you shouldn’t use a for each loop with a index counter as you are here.
Per the language ref: “A For…Each loop does not guarantee that it will loop through the values in the array in index order. Do not make any assumptions of the traversal order as it is subject to change in the future.”

So, use a traditional for loop, but be sure to loop from ubound downto 0 in order to avoid out of bounds exceptions after you remove elements from the array.

So something more like:

for i as integer = App.PictureArray.Ubound downto 0 if uuid = App.PictureArray(i).Value("uuid") then picture = App.PictureArray(i).Value("picture") App.PictureArray.Remove(i) end next[/quote]

If App.PictureArray is an array of dictionaries, then you definitely can access the individual dictionaries and their values in that way.

Give it a try.

I stand corrected! Thank you very much! Just what I needed. Was making it more complicated than it needed to be! :slight_smile: