Probably a silly question about Arrays of Dictionaries

Hi everyone,
I’m very new to Xojo (4 days) and also to the programming world (I’ve previous little knowledge of C, C++, ObjC and lately Swift).
I’m trying to understand programming by myself and I’ve already accomplish something big for a real newbie like me.
I’ve developed a few desktop and iOS apps and an easy game based on colors and I would like to save infos of the game in an array of dictionaries and show them in a listbox, like a hall of fame
To understand better, I’ve started from scratch in a new project with three fields and two buttons: Add item and Open Table
I’ve created a module with a global array and a dictionary. I’ve filled the text field and when I’ve pressed the button has assigned the value of the text fields to the dictionary.
After that has added the dictionary to the array.

[code] myDictionary.Value(“nome”)= NomeField.Text

myDictionary.Value(“cognome”)= CognomeField.Text

myDictionary.Value(“et”)=EtaField.Text

myArray.Append(myDictionary)[/code]

Another button opens a window with a listbox. On the listbox open event there’s a for each loop to add a row for every single key.

for each element as Dictionary in myArray Listbox1.addRow(element.Value("nome").stringValue, element.Value("cognome").stringValue, element.Value("et").stringValue) next

When I’ve run the app and I’ve started to add values in the text fields and to the array I’ve tried to press the button to fill the list and all the rows were the same.
I think that is something relate to object-referencing. Every instance of the dictionaries stored in the array are pointing to the same location in memory hence the list is all the last entry.
While I was debugging I saw that, when the second append function takes place, the first index changes value as well.
I’ve read a lot before asking for help but I’m really puzzled, please give me a hint to get out of the pit!!
Thanks in advance

P.S: I’ve also added myDictionary = new Dictionary to the App Open event

STEFANO

Its the same dictionary every time.
You need to have a
myDictionary = new Dictionary at the start of your code.
Then each new dictionary will be added to the array, and lines like

myDictionary.Value(“nome”)= NomeField.Text will affect DIFFERENT dictionaries, not ‘the same one every time’

I think for this purpose you would be better off with a class with 3 properties.
Each time, start a new instance of the class,
set the properties
add the object to the array

Then iterate through the objects later on

[quote=290252:@Jeff Tullin]Its the same dictionary every time.
You need to have a
myDictionary = new Dictionary at the start of your code.
Then each new dictionary will be added to the array, and lines like

myDictionary.Value(“nome”)= NomeField.Text will affect DIFFERENT dictionaries, not ‘the same one every time’

I think for this purpose you would be better off with a class with 3 properties.
Each time, start a new instance of the class,
set the properties
add the object to the array

Then iterate through the objects later on[/quote]

Sorry I forgot to mention that I did create the dictionary in the App Open Event

Thanks Jeff, you’ve forced my brain to connect. I’ve moved the myDictionary = new Dictionary to the add Item action, before the dictionary assigning value process and voil… problem solved!
Thanks again

P.S. This forum rocks. Your answer speed was astonishing :slight_smile: