Class in a dictionary value always point to the same data

Hi

I’m playing now since almost 2 years with xojo, doing some prototypes on desktop and web. Now I’m working on the first realworld desktop app. I’m hanging with classes as value in a dictionary.

I have reading this blog Xojo Blogpost and followed in this forum post jeff’s car example.

Context:
I’m writing a software to manage a sportsevent. The data of each event will be stored in a json-file. When the software starts, the user choose a data-file (json) for the actual event. In the data-file will be information about the event (date, name, judge and so on). But also arrays of data like categories, classes (beginners, pro ..), participants, results. The datas should be stored globaly in dictionaries, so that all windows/methodes can worked with them.

I can load the eventinformations from the json to a dictionary and load it in a window into a ValueListBox. This is handled as key-value list. It works.

But there are the arrays in the json.

Problem:
I had took jeffs example and changed.So I created a class for Event Classes call “EventClass“. This class has properties like “class_id“, “class_name”. Then I created a property “EventClassDic“ from type dictionary. And I create a instance of “EventClassDic“ by adding “EventClassDic = New Dictionary“ in the opening Handler of the app.

When the user opens the datafile, all classes should be loaded to the dictionary. The class_id will be the key of the dictionary, the value should be a new “EventClass” Object. The properties of the “EventClass“-Object gets the data (class_id, class_name) from the json-object. and then the Class will be stored as value in the dictionary.

Var classesArr As JSONItem = root.Value("classes")

For i As Integer = 0 To classesArr.Count - 1
  Var cObj As JSONItem = classesArr.Child(i)
  
  var cClass as Globals.EventClass
  cClass = new Globals.EventClass
  cClass.class_id = cObj.Value("class_id")
  cClass.class_name =cObj.Value("class_name")
  EventClassesDic.value(cObj.Value("class_id")) = cClass
   
Next

In a other window I would like to show the data of the dictionary.value (= class “EventClass”)

ValuesListBox.removeAllRows


For Each entry As DictionaryEntry In EventClassesDic
  
  system.debugLog("Class ID im Dic " + entry.key)
  
  
  If Not EventClassesDic.HasKey(entry.key) Then
    system.debugLog("Nicht gefunden '" + entry.key )
    
    
  else
    
    var oClass As Globals.EventClass = EventClassesDic.Value(entry.key)
    
    system.debugLog("Id im Objekt " + oClass.class_id)

    ValuesListBox.AddRow oClass.class_name
    ValuesListBox.RowTagAt(ValuesListBox.lastAddedRowIndex) = oClass.class_id
    
  End If

But as result/output I get a list of the same eventclass (“Plausch“).

You see in the code that I have output some log: so i see the dictionary is filled correctly with the key (=class_id). But not correct with the value (class “EventClass“).

It looks that I have a missing a thing in the code to create a new class for every entry.

Can anyone help?
Daniel

Var cclass as new eventclass

The code above is creating new instances. That’s just a style choice.

var cClass as Globals.EventClass
cClass = new Globals.EventClass

An issue similar to this was posted on Stack Overflow some time ago, but communication tools are limited there. I would be interested to see an example project of this happening.

1 Like

Also: have you debugged this to ensure that you are getting new data for each iteration?

1 Like

here is a sample project:

sample-xojo.zip (16.1 KB)

Yes, json should be loaded correctly:

Had a quick look. The loaded data is okay. But when I step through ClassesWindow.opening every entry in EventClassesDic has the same value.

What is the reason to have EventClass in the Globals module?

Why do you use the complex Json structure instead of an SQLite database? With an SQLite database you get flat data out of more complex SQL which is way easier to handle than classes as value of a dictionary.

Nope, the data is not loaded correctly. This is EventClassesDic:

But every value of EventClass has the same data:

I also wouldn’t load the data twice where the key is also in the value of the class.

1 Like

I found the issue.
Your EventClass only has Shared properties.

You are actually setting the shared properties class_id and class_name for each instance as they are Shared properties.

Change those to Regular properties (last item in the right-click menu) and you won’t have the issue anymore

4 Likes

Thanks Jeremie. That was the problem. Now it runs like expected:

1 Like