Dictionary lookup returning NaN

In my code I do the following:

Dim d as New Dictionary
d.value(“MyNumber”)=0.0

Later in my code, I wish to retrieve this value so, I have
dim num as double
num=d.Lookup(“MyNumber”,0.0)

However, if I put a Breakpoint in my code at the next line, I will see that
num = NaN

why is this? Why isn’t num=0?

I suggest you have something happening between the two points where you add the value and retrieve it, that is changing the dictionary entry.

I entered just the lines you had above and it worked just fine.

  Dim d as New Dictionary
  d.value("MyNumber")=0.0
  
  dim num as double
  num=d.Lookup("MyNumber",0.0)

I personally dislike “Lookup” because I personally dislike “variants”

Try this instead

num=d.value("MyNumber")

by default it returns 0 for non-found numerics, and empty string for non-found string

A Double as key may be bad as double comparison may not give expected results.

except a STRING is being used as a key in this example

sorry. so maybe something else overwrote it…

Could it be a scope issue?

[quote=290385:@Dave S]Try this instead

num=d.value(“MyNumber”)
by default it returns 0 for non-found numerics, and empty string for non-found string[/quote]
Huh? Should throw an exception.

Besides, both Lookup and Value return a variant. So I don’t see that there’s much difference. (Except that Lookup won’t throw an exception and take your app down in a ball of flame.)

Dave’s comment made me go through my code and sure enough, the value is getting rewritten in a part of the code that I didn’t think would be run until later (it is buried within a canvas paint event).
Once again, my code has done what I told it to do and NOT what I want it to do…
Thank you all for your comments.

I tested it before I posted it…

How did you test it? I get a KeyNotFoundException…

[code]dim d As new Dictionary // or Xojo.Core.Dictionary

dim num As double = d.Value(“MyNumber”)[/code]

see the code I posted above

Oh, I was testing this…

That other code puts the key in there. For non-found keys there’s an exception.

I meant your comment about not-found. If the key isn’t in the dictionary, Value() throws an exception. Lookup doesn’t.