# For Each bug when looping over a Dictionary

**URL:** <https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859>\
**Category:** General\
**Created:** [May 23, 2023, 11:13am UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859 "2023-05-23T11:13:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Louis\_Brauer1](https://forum.xojo.com/user_avatar/forum.xojo.com/louis_brauer1/32/9058_2.png) [@Louis\_Brauer1](https://forum.xojo.com/u/Louis_Brauer1)\
**Post date:** [May 23, 2023, 11:13am UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/1 "2023-05-23T11:13:53Z")

</div>

Hi there

Using Xojo 2023r1, why would this code not yield an error during Compile time?

```auto
Var myDict As New Dictionary

myDict.Value("de") = "Deutsch"
myDict.Value("en") = "English"
myDict.Value("fr") = "Français"

For Each key As String In myDict
  ListBox1.AddRow myDict.Value(key).StringValue
Next

```

As you may have spotted, I accidentally looped over a `Dictionary` (instead of using its `Keys` method).

A `Dictionary` is neither a `String` array not does it implement the `Iterable` interface. In my opinion, this should yield a “_This object does not implement the Iterable interface and cannot be used in a For Each loop_” error during compile time.

Do I overlook something here?

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 23, 2023, 11:31am UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/2 "2023-05-23T11:31:36Z")

</div>

Well… You stumbled on something, but not what you think

 ![image](https://forum.xojo.com/uploads/default/original/2X/a/a01b739e8beb9bd66e48c06e2392714f7c3ab6e9.png)

Dictionaries are iteratable BUT their items aren’t strings, they are DictionaryEntry objects

In your example the compiler should point out the incompatibility in the “key” declaration as String instead of the expected DictionaryEntry. Compiling it and raising a crazy exception later is an issue needing a report.

```auto
Var myDict As New Dictionary

myDict.Value("de") = "Deutsch"
myDict.Value("en") = "English"
myDict.Value("fr") = "Français"

For Each entry As DictionaryEntry In myDict
  System.DebugLog entry.Value
Next

---

Run:
08:28:10 : test Launched
08:28:11 : Deutsch
           English
           Français

```

---

<div class="post-metadata">

**Author:** ![Louis\_Brauer1](https://forum.xojo.com/user_avatar/forum.xojo.com/louis_brauer1/32/9058_2.png) [@Louis\_Brauer1](https://forum.xojo.com/u/Louis_Brauer1)\
**Post date:** [May 23, 2023, 11:46am UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/3 "2023-05-23T11:46:39Z")

</div>

Well, thank you Rick!

Then I correct my assumption in that Xojo should yield a “_Type mismatch_” compile time error, since a `DictionaryEntry` (which I didn’t even realize existed) is not assignable to a `String`.

I’m a little sad that, given the Documentation and the lack of a compile-time error (and since I was working inside a web app, I didn’t even see the Runtime error which made me spend a lot of time bug hunting), I was not able to infer that.

The documentation is unsurprisingly bad regarding this feature:

1. In “[For Each … Next](https://documentation.xojo.com/api/code_execution/for_each...next.html)” there is no mention and no example at all.

2. In “[Iterable](https://documentation.xojo.com/api/code_execution/iterable.html)”

![image](https://forum.xojo.com/uploads/default/original/2X/1/1c61d1e39c34daaa705aefabfb07cb8b6a14475d.png)

1. In “[Dictionary](https://documentation.xojo.com/api/language/dictionary.html)”:

There I found an actually working example under the “Value”(?) method:

![image](https://forum.xojo.com/uploads/default/original/2X/7/7d8d2e8addfa74c3f6c6eef44fcf65b72e170b4e.png)

I might open a bug so this part of the documentation can be improved. Thank you again, Rick, for your fast response.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 23, 2023, 11:55am UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/4 "2023-05-23T11:55:36Z")

</div>

The docs are always needing improvements. There are lots of hidden features that could be better documented, sometimes in more than one place as a cross-reference citation.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 23, 2023, 2:20pm UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/5 "2023-05-23T14:20:25Z")

</div>

> [@Rick\_Araujo](#):
>
> In your example the compiler should point out the incompatibility in the “key” declaration as String instead of the expected DictionaryEntry. Compiling it and raising a crazy exception later is an issue needing a report.

Can’t the key be a Variant? Seems to me the loop would work until a non-string key is encountered (untested). Thus, not detectable at compile time.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [May 23, 2023, 4:07pm UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/6 "2023-05-23T16:07:19Z")

</div>

The Key is a Variant, yes, but the iterable items of a dictionary are DictionaryEntries. You can of course iterate over the array of keys as well.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [May 23, 2023, 4:15pm UTC](https://forum.xojo.com/t/for-each-bug-when-looping-over-a-dictionary/75859/7 "2023-05-23T16:15:57Z")

</div>

> [@TimStreater](#):
>
> Can’t the key…

It’s a different construct using keys, they can for sure be variants, but it’s unnecessary and less performant then directly using strings if the keys are strings:

```auto
Var myDict As New Dictionary

myDict.Value("de") = "Deutsch"
myDict.Value("en") = "English"
myDict.Value("fr") = "Français"

For Each key As String In myDict.Keys
  System.DebugLog key + " : " + myDict.Value(key)
Next

System.DebugLog "---"

For Each entry As DictionaryEntry In myDict
  System.DebugLog entry.Key + " : " + entry.Value
Next

```
