From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Class (inherits from Object)

The Dictionary class is an unordered, mutable, key-value store that is loosely typed. It implements the Iterable interface, allowing efficient and easy iteration over all key-value pairs.

Count fa-lock-32.png
Clone Lookup Value
GetIterator Remove
HasKey RemoveAll


As shown on GetIterator, if you change the Dictionary while iterating over it using For Each...Next, an exception will be raised. If you find you need to iterate and change the data, you can add a method to get all the keys into an array and then iterate through the array to access the Dictionary values. A method could be something like this:

Function EagerlyEvaluateIterable(obj As Xojo.Core.Iterable) As Auto()
Var results() As Auto
For Each item As Auto In obj
Return results
End Function

Now you can call the method to get an array where you can then modify the contents:

For Each entry As Xojo.Core.DictionaryEntry In EagerlyEvaluateIterable(d)
' Stuff that can mutate the dictionary

Sample Code

Add items to a Dictionary and loop through them:

Var months As New Xojo.Core.Dictionary
months.Value("January") = 31
months.Value("February") = 28
months.Value("March") = 31
months.Value("April") = 30
months.Value("May") = 31
months.Value("June") = 30
months.Value("July") = 31
months.Value("August") = 31
months.Value("September") = 30
months.Value("October") = 31
months.Value("November") = 30
months.Value("December") = 31

For Each days As Xojo.Core.DictionaryEntry In months
Var numDays As Integer = days.Value

See Also

Auto data type; Xojo.Core.DictionaryEntry, KeyNotFoundException, Dictionary classes