Dictionary in Dictionary problems

The forum helped me to get my XMLReader to work a while back but I just now noticed a problem. This is how I am handling the xml in my XMLReader…

?xml version="1.0" encoding="UTF-8"?> <schools> <details> <name>Academy of the Pacific</name> <id>1</id> <districtid>1</districtid> <rank>0</rank> </details> <details> <name>Ahuimanu Elementary</name> <id>107</id> <districtid>6</districtid> <rank>0</rank> </details> </schools>

The goal here was to put each school in a dictionary and then put them all into a single dictionary. To do this I created the follwoing properties in my XMLReader


I call the reader like this…

    Dim reader As New myXMLReader
    reader.Output = session.Schools   //a dictionary
    reader.TypeKey = "schools"
    reader.DetailsKey = "details"

Then my event handlers handle the stream like this…

  Key = name
  If Key = DetailsKey Then
    Dim d As New Dictionary
    detailsDictionary = d
  End If
    If Key <> TypeKey And Key <> DetailsKey Then
    End If
  If name = DetailsKey Then
        output.Value(detailsDictionary.Value("id"))= detailsDictionary  
  End If

I thought was working just fine. I realized this morning, however, that later on in the session I am randomly losing the first dictionary added to the Schools dictionary by the XMLReader.

I think I understand what is happening when I put the a detailsDictionary into the Output dictionary I am actually putting a reference to the memory allocation for detailsDictionary. Somewhere along the way that memory block is getting changed. It is not nil, the dictionary is just empty.

Any ideas? Perhaps my whole approach above is flawed.

Since you use .session I presume this should be in the WEB forum?

I don’t use web but as you refer to a session you might have a problem when creating the “new” xmlreader, so it could be a problem with object referencing.

But the random nature of the problem does not quite fit. Could it be that when you do the lookup that some keys that you expect to be there aren’t there with some data sets?

is the id of the first school being reused by another school?

If Key = DetailsKey Then Dim d As New Dictionary detailsDictionary = d End If

does d go out of scope after the end if?
if so,

If Key = DetailsKey Then detailsDictionary = New Dictionary End If

I love dictionaries too but wouldn’t it be easier to just create a structure?

Thanks for the replays. I found the problem. It is what I suspected with another process clearing the dictionary via object referencing. I have a method that establishes a current school and copies the appropriate school dictionary out to current School dictionary just to make it easier to extract data for that school while that school is current. I was unecessarily clearing the currentSchool Dictionary before establishing a new current school. So the first school to become current by default happens to be the first school added to the Schools dictionary. As I understand things, currentSchool holds a reference to the dictionary in the Schools dictionary and so clearing out currentSchool cleared out the referenced dictionary in the Schools dictionary.

Very interesting and eductational for me, but also very confusing.

BTW, I did not realize that perhaps I should be posting on the web forum. If so I will do so from now on, but thanks to you all for the help this past couple of weeks. I have never been able to get so much done in a new development environment as I have with Xojo. That says a lot for Xojo as well as this forum.