Xojo.Core.Iterable.GetIterator

From Xojo Documentation

Revision as of 19:58, 23 August 2018 by PLefebvre (talk | contribs) (Sample Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Method

Xojo.Core.Iterable.GetIterator() As Xojo.Core.Iterator

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:

Dim entries() As Xojo.Core.DictionaryEntry
For Each entry As Xojo.Core.DictionaryEntry In d
TextArea1.Text = TextArea1.Text + " " + 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 Xojo.Core.Iterable) As Auto()
Dim results() As Auto
For item As Auto In obj
results.Append(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 Xojo.Core.DictionaryEntry In EagerlyEvaluateIterable(d)
// Stuff that can mutate the dictionary
Next

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

Dim docs As Xojo.IO.FolderItem
docs = Xojo.IO.Special.Folder.Documents

Dim files() As Text
For Each f As Xojo.IO.FolderItem In docs.Children
files.Append(f.Name)
Next