Trouble retrieving a dicionary from a dictionary with lookup

Dim k As Variant k = 107 Dim school as Dictionary school.Value("Test") = "testing" Dim dict As Dictionary dict =schoolsList.Lookup(k,school)

In the above simplified code, schoolsList is a dictionary containing dictionaries of schools (ID, name, address, phone, etc.). The schoolsList dictionary is created using XML returned from a database and it’s keys are the ID numbers of the schoools. I know that there is a key (ID) 107 in the schoolsList dictionary.

Why doesn’t this work. No errors. I always get the default, which must be because the key 107 was not found.

I thought that maybe the problem lay in the fact that I was taking the ID as a string as parsed from the xml from the database. Maybe the ID had some invisible character like an EndOfLine. So I changed the parsing code to CDbl the ID before creating the key value pair, thus as a number instead of a string insuring that their could not be any extraneous invisible characters. That did not work, I still get the default dictionary.

What am I doing wrong?


Why not post the actual code?

What I posted is in fact the actual code I am usinig to debug the problem. I always simplify when I have a problem.
The only thing I did not show you is the XMLReader code that fills the schoolsList dictionary…

If you think it will help…

  Key = name  //Key is a property of my XMLReader subclass
  If Key = DetailsKey Then   //DetailsKey is a property of my XMLReader sublass and is the start element of a school in the XML
    Dim d As New Dictionary
    detailsDictionary = d  //detailsDictionary is a property of my XMLReader subclass
    Key = name
  End If
    If name = DetailsKey Then
        output.Value(CDbl(detailsDictionary.Value("id")))= detailsDictionary 
           //output is a dictionary property of my XMLReader subclass that is set by the caller to the schoosList dictionary 
           //here is where I converted string ID to a number to make sure we don't have any invisible characters in the Key
      End If

I would change this line:

output.Value(CDbl(detailsDictionary.Value("id")))= detailsDictionary


output.Value(detailsDictionary.Value("id").IntegerValue) = detailsDictionary

Then I’d change the code in the original post to:

Dim k As Integer = 107 // make this an Integer Dim school As New Dictionary() school.Value("Test") = "testing" Dim dict As Dictionary = schoolsList.Lookup(k, school)

@Eli Ott

Your a genius! :slight_smile:

Makes sense too. Cdbl does not create an integer and I was trying to use an integer in k.

That worked great, but I thought thought after reading the docs that the lookupu, k in this case, had to be typed as a variant because Keys are variants. As it turns out it actually works, either way. I am guessing that if you know what type value was used as the key you can use it for the lookup paramater. If you don’t know you should use variant?

Eli, Thank you. It’s getting late and I hate to go to be frustrated.


You should never use a variant when you know which type a variable will hold. Better use that type do dim the variable.

The reason that methods like Dictionary.Value and Dictionary.Lookup take a variant is not because you should use a variant (though you can) but because you can use an arbitrary type as argument.

In my applications I never dim any variable to hold a variant. To me variants only make sense as parameters for methods because Xojo doesn’t have Generics. Without Generics the only way to achieve type-safety would be to overload methods for each data type, which is impractical.