Iterable.Iterator

From Xojo Documentation

Method

Iterable.Iterator() As Iterator

New in 2019r2

Supported for all project types and targets.

Returns an Iterator that can be used with For Each...Next loops.

Sample Code

Classes that implement Iterable, such as Dictionary, can be used with For..Each loops:

Var entries() As DictionaryEntry
For Each entry As DictionaryEntry In entries
TextArea1.Value = TextArea1.Value + " " + entry.Key + " " + entry.Value
Next

Note that if you change an Iterable while iterating over it using For Each...Next, an exception is 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 values. A method could be something like this:

Function EagerlyEvaluateIterable(obj As Iterable) As Variant()
Var results() As Variant
For Each item As Variant In obj
results.Add(item)
Next
Return results
End Function

Now you can call the method to get an array where you can then modify the contents. For example. this would let you iterate and modify a Dictionary:

For Each entry As DictionaryEntry In EagerlyEvaluateIterable(d)
// Stuff that can mutate the dictionary
Next

You can also use FolderItem.Children to loop through the files in a folder:

Var docs As FolderItem
docs = Special.Folder.Documents

Var files() As String
For Each f As FolderItem In docs.Children
files.Add(f.Name)
Next