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?
DaveS
(DaveS)
October 6, 2016, 10:51pm
2
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.
DaveS
(DaveS)
October 6, 2016, 11:17pm
4
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?
Tim_Hare
(Tim Hare)
October 6, 2016, 11:39pm
7
[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.
Tim_Hare
(Tim Hare)
October 6, 2016, 11:41pm
8
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.
DaveS
(DaveS)
October 7, 2016, 4:18am
10
I tested it before I posted it…
Will_Shank
(Will Shank)
October 7, 2016, 4:30am
11
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]
DaveS
(DaveS)
October 7, 2016, 4:31am
12
see the code I posted above
Will_Shank
(Will Shank)
October 7, 2016, 5:15am
13
Oh, I was testing this…
That other code puts the key in there. For non-found keys there’s an exception.
Tim_Hare
(Tim Hare)
October 7, 2016, 6:09am
14
I meant your comment about not-found. If the key isn’t in the dictionary, Value() throws an exception. Lookup doesn’t.