<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Iterable &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/iterable/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Fri, 04 Feb 2022 16:57:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Iterating Through a Date Range</title>
		<link>https://blog.xojo.com/2022/02/07/iterating-through-a-date-range/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Mon, 07 Feb 2022 14:50:00 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Beginner Tips]]></category>
		<category><![CDATA[Iterable]]></category>
		<category><![CDATA[Iterator]]></category>
		<category><![CDATA[Multi-Platform Development]]></category>
		<category><![CDATA[Rapid Application Development]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=9951</guid>

					<description><![CDATA[The Iterable and Iterator interfaces are a great way to simplify code. For example, what if you want to iterate through dates in a range? ]]></description>
										<content:encoded><![CDATA[
<p>The <a href="https://documentation.xojo.com/api/code_execution/iterable.html">Iterable</a> and <a href="https://documentation.xojo.com/api/code_execution/iterator.html">Iterator</a> interfaces are a great way to simplify code. For example, what if you want to iterate through dates in a range? Perhaps you&#8217;d like to write code like this:</p>



<pre class="wp-block-preformatted">For Each day As DateTime In startDate.Range(endDate)
  ListBox1.AddRow(day.ToString)
Next</pre>



<p>Although that is not part of the Xojo language, you can add it yourself using these Xojo features:</p>



<ul class="wp-block-list"><li><a href="https://documentation.xojo.com/api/language/extends.html">Extension method</a></li><li><a href="https://documentation.xojo.com/api/code_execution/iterable.html">Iterable interface</a></li><li><a href="https://documentation.xojo.com/api/code_execution/iterator.html">Iterator interface</a></li></ul>



<p>First, create an extension method in a module like this (make sure the method is Global):</p>



<pre class="wp-block-preformatted">Range(Extends startDate As DateTime, endDate As DateTime) As Iterable
  Return New DateTimeIterable(startDate, endDate)</pre>



<p>Now add a new class, called <strong>DateTimeIterable</strong>. In its Inspector, click Interfaces and choose Iterable and click OK.</p>



<p>Your class now has an <strong>Iterable</strong> method on it. Before implementing that, add a <strong>Constructor</strong>:</p>



<pre class="wp-block-preformatted">Constructor(startDate As DateTime, endDate As DateTime)
  mStartDate = startDate
  mEndDate = endDate</pre>



<p>Now create the two private properties:</p>



<ul class="wp-block-list"><li>mStartDate As DateTime</li><li>mEndDate As DateTime</li></ul>



<p>Lastly, implement the Iterator method:</p>



<pre class="wp-block-preformatted">Return New DateTimeIterator(mStartDate, mEndDate)</pre>



<p>With that done, now create the <strong>DateTimeIterator</strong> class and set its Interface to Iterator, which adds two methods: <strong>MoveNext</strong> and <strong>Value</strong>.</p>



<p>Before implementing those, add the <strong>Constructor</strong>:</p>



<pre class="wp-block-preformatted">Constructor(startDate As DateTime, endDate As DateTime)
  // Have to set the first date to be 1 before the actual first date
  // so that when it is first iterated it starts where we want.
  Var oneDay As New DateInterval
  oneDay.Days = 1

  mCurrentDate = startDate - oneDay
  mEndDate = endDate</pre>



<p>Now create the private properties:</p>



<ul class="wp-block-list"><li>mEndDate As DateTime</li><li>mCurrentDate As DateTime</li></ul>



<p><strong>MoveNext</strong> moves to the next item in the iterator and returns True. If there is no next item it returns False. Its code:</p>



<pre class="wp-block-preformatted">Var oneDay As New DateInterval
oneDay.Days = 1

mCurrentDate = mCurrentDate + oneDay

If mCurrentDate &lt;= mEndDate Then
  Return True
Else
  Return False
End If</pre>



<p>The <strong>Value</strong> method returns the current iterator value. Its code:</p>



<pre class="wp-block-preformatted">Return mCurrentDate</pre>



<p>That is it. With this setup you can now write code like this to populate a ListBox with all the days in January 2022:</p>



<pre class="wp-block-preformatted">Var startDate As New DateTime(2022, 1, 1)
Var endDate As New DateTime(2022, 1, 31)

For Each day As DateTime In startDate.Range(endDate)
  ListBox1.AddRow(day.ToString)
Next</pre>



<p><a href="http://files.xojo.com/BlogExamples/DateIterator.xojo_binary_project">Download the sample project.</a> I hope you find this technique useful.</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="686" height="1024" src="https://blog.xojo.com/wp-content/uploads/2022/02/CleanShot-2022-02-02-at-17.41.30@2x-686x1024.png" alt="" class="wp-image-9952" srcset="https://blog.xojo.com/wp-content/uploads/2022/02/CleanShot-2022-02-02-at-17.41.30@2x-686x1024.png 686w, https://blog.xojo.com/wp-content/uploads/2022/02/CleanShot-2022-02-02-at-17.41.30@2x-201x300.png 201w, https://blog.xojo.com/wp-content/uploads/2022/02/CleanShot-2022-02-02-at-17.41.30@2x-768x1146.png 768w, https://blog.xojo.com/wp-content/uploads/2022/02/CleanShot-2022-02-02-at-17.41.30@2x.png 996w" sizes="(max-width: 686px) 100vw, 686px" /></figure></div>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Updated: Make Your Own Classes Iterables</title>
		<link>https://blog.xojo.com/2021/09/02/updated-make-your-own-classes-iterables/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Thu, 02 Sep 2021 19:59:59 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[AprendeXojo]]></category>
		<category><![CDATA[Iterable]]></category>
		<category><![CDATA[Iterator]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=9074</guid>

					<description><![CDATA[In programming, iterators are the mechanisms that allow us to run through all the items of a collection without needing to know in advance how many of them compose that collection. To do this, we can use the commands For Each… Next in Xojo. What are the main differences when comparing For Each… Next with the conventional For… Next? See how the For Each… Next loop iterates every item associated with the object. It's a more concise, stylish and Object-Oriented (OOP) way of doing things when compared with the classic For… Next loop.]]></description>
										<content:encoded><![CDATA[
<h5 class="wp-block-heading">This blog post was <a href="https://blog.xojo.com/2017/12/13/make-your-own-classes-iterables/">originally written</a> by Javier Menendez in 2017 and has been updated for Xojo API 2.0 by Wayne Golding.</h5>



<p>In programming,&nbsp;<a href="https://documentation.xojo.com/api/code_execution/iterator.html">iterators</a>&nbsp;are the mechanisms that run through all the items of a collection without needing to know in advance how many items compose that collection. To do this in Xojo, use the commands&nbsp;<code>For Each… Next</code>. What are the main differences when comparing <code>For Each… Next</code> with the conventional&nbsp;<code>For… Next</code>?</p>



<p>The first difference is that with&nbsp;<code>For Each… Next</code>&nbsp;we can’t assume that we are iterating the items in the collection in order, as is the case when using the variable of the conventional&nbsp;<code>For… Next</code>. The second difference is that the iterator will be invalid when the iterated elements are modified or when we modify the number of elements in the collection during the iteration process.</p>



<p>By default in Xojo, there are a couple of collections that are <a href="https://documentation.xojo.com/api/code_execution/iterable.html">iterable</a>: <a href="https://documentation.xojo.com/api/language/array.html">Arrays</a>, <a href="http://developer.xojo.com/xojo-core-dictionary">Dictionaries</a> and <a href="https://documentation.xojo.com/api/files/folderitem.html.Children">FolderItem.Children</a>. Wouldn’t it be great to extend this behaviour to other classes, making them more flexible? The key to doing just that is in two of Xojo&#8217;s Class Interfaces: Iterator and Iterable.</p>



<p><a href="https://www.dropbox.com/s/gcjvnmewyc3wyb4/Ejemplo_Iterador.zip?dl=1">Download the example project</a> used in this tutorial.</p>



<h3 class="wp-block-heading"><strong>Using Iterator</strong></h3>



<p>In order to implement this behaviour in our classes so they can use the&nbsp;<code>For Each… Next</code>&nbsp;schema, the real work is in the&nbsp;Iterator&nbsp;Class Interface. Under this interface, we can find the definition of the methods that are needed to implement the class and that are responsible for advancing every item of the collection, and for returning the current value of the collection:</p>



<ul class="wp-block-list"><li><strong>MoveNext</strong>. Moves to the next element of the Collection, returning <code>True</code> if it has not reached the end of the collection, <code>False</code>when reached past the last element in the Collection.</li><li><strong>Value</strong>&nbsp;This is the Class Interface method that returns the current element of the collection as an Variant data type.</li></ul>



<p>From this, it is easy to conclude that our iterable classes need to implement a counter (as a Property) that updates its value for every&nbsp;<code>MoveNext</code>&nbsp;invocation. We will also use this property to check if it has reached the maximum number of elements. Also, be aware that the property has to be initialized using the value -1 (that is, as an invalid value), as it is stated in the documentation that we have to invoke <code>MoveNext</code> before we can get the first iterable element from the collection.</p>



<p>On the other hand, we will handle all the error cases during the iteration process raising <code>IteratorException</code>.</p>



<p>In order to see all this in action, let’s run through an example using the <code>Person</code>class in charge to store in their instances all the possible postal addresses associated with a person.</p>



<p>Let’s start defining our<code>Person</code>class as a simple set of Properties:</p>



<ul class="wp-block-list"><li>firstName As String</li><li>middleName As String</li><li>secondName As String</li></ul>



<p>Also, add the Property in charge of referencing all the postal addresses assigned to the Person, using an Array:</p>



<ul class="wp-block-list"><li>postalAddresses() As PostalAdress</li></ul>



<p>With that, we have finished our Person class definition. It’s very simple but works for the purpose of this example.</p>



<p>We will also create a simple (and incomplete) definition for the PostalAddress class using a set of public properties so they serve the purpose of the main topic.</p>



<ul class="wp-block-list"><li>street As Text</li><li>streetNumber As Integer</li><li>streetFlat As Integer</li></ul>



<h3 class="wp-block-heading"><strong>Creating an Iterator</strong></h3>



<p>Once we have the two classes needed for our example (Person and PostalAddress), we can define the Class responsible for acting as the iterator; that is, returning every element from the collection for the associated class. In this example we will return every object of the PostalAddress that is associated with an instance of the Person class. In fact, the Iterator is responsible of doing all the hard work, moving forward the counter to the next item, and returning the object or value when requested.</p>



<p>Let’s create a new Class with the name<code>PersonIterator</code>. In this case, use the associated Inspector to select the<code>Choose</code>button next to the<code>Interfaces</code>label. This will display a window containing all the available Class Interfaces. Select the checkbox for<code>Iterator</code>and confirm the selection.</p>



<p>The IDE will now add the following defined methods for the selected class interface:</p>



<ul class="wp-block-list"><li><strong>MoveNext</strong>. The documentation states that this is the method invoked before calling the Value method in order to get a new value. In fact, in the method implementation we have to write the code that increases the counter value, and also checks if the counter value is over the available quantity of items. Meaning, we have to add a<code>counter</code>Property to our class, and initialize it with the value -1.</li><li><strong>Value</strong>. This is the method that will be called every time we need to get a new value. In our example, this will return a new PostalAddress object from the available items.</li></ul>



<p>Thus, the implementation for the<code>MoveNext</code>method could be the following:</p>



<pre class="wp-block-preformatted">counter = counter + 1</pre>



<p>But the method signature and the class interface documentation states that the method has to return a boolean value of<code>True</code>if there are remaining available items to iterate and<code>False</code>if the quantity of iterable items has been surpassed. In the case we should raise an exception in order to point this out.</p>



<p>For example, our class could check if the counter doesn’t exceed the quantity of items stored by the PostalAddresses Array, returning the corresponding boolean value.</p>



<p>But let’s fill in some holes before writing the final implementation for the<code>MoveNext</code>method. First, let’s add some more needed properties:</p>



<ul class="wp-block-list"><li><strong>maxPostalAddresses As Integer</strong>. This will be responsible for storing the number of elements available under the PostalAddress Array.</li><li><strong>source As Person</strong>. This will be in charge of saving the reference to the Person object it has to iterate.</li><li>The Calculated Property&nbsp;<strong>valid as Boolean</strong>. We will use the<code>Get</code>method from this calculated property to know if the PostalAddress Array has been modified during the iteration of the object, returning<code>False</code> when this is the case. In fact, this is the code for the<code>Get</code>method:</li></ul>



<pre class="wp-block-preformatted">Return if(source.postalAddresses.LastIndex = maxPostalAddresses, True, false)</pre>



<p>Next, we will add the<code>Constructor</code>method to the class using this signature:</p>



<pre class="wp-block-preformatted">Constructor( value As Person )</pre>



<p>and the following code for the method:</p>



<pre class="wp-block-preformatted">source = value
maxPostalAddresses = value.postalAddresses.LastIndex</pre>



<p>This initialized the previously declared property’s values with the reference to the Person object that we will use to iterate over, and the number of elements included in the Array when we initialize the class. Thus, the number of original items that we will use to check if it has been modified during the iteration process.</p>



<p>Now we can move back to the<code>MoveNext</code>method to write the final implementation:</p>



<pre class="wp-block-preformatted">If valid Then
  counter = counter + 1
  Return If(counter &lt;= source.postalAddresses.Ubound, True, False)
Else
&nbsp; Var e As New IteratorException
  e.Reason = "Invalid Data"
  Raise e
End If</pre>



<p>Inside the method implementation we will also check if the object has been modified during the iteration, returning the corresponding value:</p>



<pre class="wp-block-preformatted">If valid Then
  Return source.postalAddresses(counter)
Else
  Var e As New IteratorException
  e.Reason = "Invalid Data"
  Raise e
End If</pre>



<p>As you can see, the Value method will return the PostalAddress item from the Person array, using the value of the Counter property as Index.</p>



<p><strong>Creating the Iterable Class</strong></p>



<p>Now we have to create the iterable class itself. Add a new class to the project with the name <code>IterablePerson</code> and make it a subclass from the Person class. Write this data type in the<code>Super</code>field of the Inspector Panel. In addition, this class will be the one that implements the class interface&nbsp;Iterable, so you have to push on the<code>Choose</code>button associated with the<code>Interfaces</code>label in order to select and confirm the addition of the corresponding methods. In fact, is just one method:</p>



<pre class="wp-block-preformatted">Iterator() As Iterator</pre>



<p>And this is the code for that method:</p>



<pre class="wp-block-preformatted">Var it As New iterablePerson(Self)
Return it</pre>



<p>Now we have all the definitions we need for the subclass: we get a new <code>IterablePerson</code>instance passing as a parameter the iterable object (a Person subclass). Finally, we return the iterator we got and that will be used inside the<code>For Each… Next</code>loop.</p>



<h3 class="wp-block-heading"><strong>Putting It All Together</strong></h3>



<p>Armed with all the needed classes for our example, we can finally put it all into practice. For this, add a&nbsp;<a href="http://developer.xojo.com/listbox">ListBox</a>&nbsp;in the main project Window (<code>Window1</code>). This is the control where we will add as many rows as there are postal addresses associated with an instance of the Person class used for the example. For this, add a button to the window with the Action event and write the following code in the resulting Code Editor:</p>



<pre class="wp-block-preformatted">Var p As New IterablePerson
p.firstName = "Juan"
p.middleName = "Sin Miedo"
p.secondName = "De Nada"
Var d As New PostalAddress
d.street = "Juan de Urbieta"
d.streetNumber = 6
d.streetFlat = 1
p.postalAddresses.Add(d)
d = New PostalAddress
d.street = "Mi calle bonita"
d.streetNumber = 8
d.streetFlat = 3
p.postalAddresses.Add(d)
d = New PostalAddress
d.street = "Princesa"
d.streetNumber = 5
d.streetFlat = 5
p.postalAddresses.Add(d)
For Each s As PostalAddress In p
  Listbox1.AddRow(s.street + ", " + s.streetNumber.ToString + " Piso: " + s.streetFlat.ToString)
Next</pre>



<p>Run the project, push the button and you will see how the<code>For Each… Next</code>loop iterates every postal address associated with the Person object. This is a more concise, stylish and OOP way of doing things when compared with the classic<code>For… Next</code>loop, which requires getting the upper value (or limit) and use a variable as index to get every collection object we are interested in.</p>



<p>*This blog post was <a href="https://blog.xojo.com/2017/12/13/make-your-own-classes-iterables/">originally written</a> by Javier Menendez in 2017 and has been updated for Xojo API 2.0 by Wayne Golding.</p>



<p><em>Wayne Golding has been a Xojo developer since 2005 and is a Xojo MVP. He operates the IT Company <a href="http://www.axisdirect.nz">Axis Direct Ltd </a>which primarily develops applications using Xojo that integrate with Xero www.xero.com. Wayne’s hobby is robotics where he uses Xojo to build applications for his Raspberry Pi, often implementing IoT for remote control.</em></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Make Your Own Classes Iterables</title>
		<link>https://blog.xojo.com/2017/12/13/make-your-own-classes-iterables/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Wed, 13 Dec 2017 06:00:16 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[AprendeXojo]]></category>
		<category><![CDATA[Iterable]]></category>
		<category><![CDATA[Iterator]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=3608</guid>

					<description><![CDATA[By default in Xojo, we can find a couple of collections that are iterable: the already mentioned Arrays and also Dictionaries. Wouldn't it be great to extend this feature so we can add this behaviour to our own classes making them more flexible?]]></description>
										<content:encoded><![CDATA[<p>In programming, <a href="http://developer.xojo.com/xojo-core-iterator">iterators</a> are the mechanisms that allow us to walk all the members of a collection without needing to know in advance how many of them compose such a collection; and for that we can find in Xojo the commands&nbsp;<code>For Each… Next</code>. What are the main differences in comparison to the conventional <code>For… Next</code>?</p>
<p>The first difference is that with <code>For Each… Next</code> we can&#8217;t assume that we are iterating the members of the collection in order, as it is the case when using the variable of the conventional <code>For… Next</code> as the Index in order to access a known member of the collection. The second difference is that the iterator will be invalid when the iterated elements are modified, or when we modify the amount of elements in the collection during the iteration process.</p>
<p>By default in Xojo, there are a couple of collections that are iterable: the already mentioned Arrays and also <a href="http://developer.xojo.com/xojo-core-dictionary">Dictionaries</a> and <a href="http://developer.xojo.com/xojo-io-folderitem$Children">FolderItem.Children</a>. Wouldn&#8217;t it be great to extend this feature so we can add this behaviour to our own classes making them more flexible? The key to making this happen is using the two Class Interfaces already included in Xojo: <b>Iterator</b> and <b>Iterable</b>.</p>
<p><span id="more-3608"></span></p>
<p>You can download the example project used by this tutorial from this <a href="https://www.dropbox.com/s/gcjvnmewyc3wyb4/Ejemplo_Iterador.zip?dl=1">link</a>.</p>
<h2>Using Iterator</h2>
<p>In order to implement this behaviour in our classes so they can use the <code>For Each… Next</code> schema, the real work is under the <a href="http://developer.xojo.com/xojo-core-iterator"><b>Xojo.Core.Iterator</b></a> Class Interface. Under this interface we can find the definition of the methods that need to implement the class and that are responsible for advancing for every item of the collection, and for returning the current value of the collection:</p>
<ul>
<li><b>MoveNext</b>. Moves to the next element of the Collection, returning<br />
<code>True</code> if it has not reached the end of the collection, or <code>False</code> when reached past the last element in the Collection.</li>
<li><b>Value</b> This is the Class Interface method that returns the current element of the collection as an Auto data type.</li>
</ul>
<p>From this, it is easy to conclude that our iterable classes need to implement a counter (Property) that updates its value for every <code>MoveNext</code> invocation. We will also use this property to check if it has reached the maximum quantity of elements. The only thing we need to be aware of is that the property has to be initialized using the value -1 (that is, as an invalid value), as it is stated in the documentation that we have to invoke <code>MoveNext</code> before we can get the first iterable element from the collection.</p>
<p>On the other hand, we will handle all the error cases during the iteration process raising an <code>IteratorException</code>.</p>
<p>In order to see all this in action, let&#8217;s run through an example using the <code>Person</code> class in charge to store in their instances all the possible postal addresses associated with a person.</p>
<p>Let&#8217;s start defining our <code>Person</code> class as a simple set of Properties:</p>
<ul>
<li>firstName As String</li>
<li>middleName As String</li>
<li>secondName As String</li>
</ul>
<p>We will add also the Property in charge of referencing all the postal addresses assigned to the Person, using an Array:</p>
<ul>
<li>postalAddresses() as PostalAdress</li>
</ul>
<p>With that, we will have finished our Person class definition. It&#8217;s very simple but will work for the purpose of the example.</p>
<p>We will also create a simple (and incomplete) definition for the PostalAddress class using a set of public properties so they serve the purpose of the main topic.</p>
<ul>
<li>street As Text</li>
<li>streetNumber As Integer</li>
<li>streetFlat As Integer</li>
</ul>
<h2>Creating an Iterator</h2>
<p>Once we have the two classes needed for our example (Person and PostalAddress), we can define the Class responsible for acting as the iterator; that is, returning every element from the collection for the associated class. In this example we will return every object of the PostalAddress that is associated with an instance of the Person class. In fact, the Iterator is responsible of doing all the hard work, moving forward the counter to the next item, and returning the object or value when requested.</p>
<p>Let&#8217;s create a new Class with the name <code>PersonIterator</code>. In this case, use the associated Inspector to push on the <code>Choose</code> button related with the <code>Interfaces</code> label. As the result of the previous action, the Xojo IDE will display a window containing all the available Class Interfaces. Select the checkbox for <b>Xojo.Core.Iterator</b> and confirm the selection.</p>
<p>As result of the previous action, the IDE will add the following defined methods for the selected class interface:</p>
<ul>
<li><b>MoveNext</b>. The documentation states that this is the method invocated before calling the&nbsp;<code>Value</code> method in order to get a new value. In fact, in the method implementation we have to write the code that increases the counter value, and also to check if the counter value is over the available quantity of items. That is, we have to add a <code>counter</code> Property to our class, and initialice it with the value -1.</li>
<li><b>Value</b>. This is the method that will be called every time we need to get a new value. In our example, this will return a new PostalAddress object from the available items.</li>
</ul>
<p>Thus, the implementation for the <code>MoveNext</code> method could be the following:</p>
<pre>counter = counter + 1</pre>
<p>But the method signature and the class interface documentation states that the method has to return a boolean value&nbsp;<code>True</code> if there are remaining available items to iterate, and <code>False</code> when we have surpassed the quantity of iterable items; and in that case we should raise an exception in order to point this out.</p>
<p>For example, our class could check if the counter doesn&#8217;t exceed the quantity of items stored by the PostalAddresses Array, returning the corresponding boolean value.</p>
<p>But let&#8217;s fill in some holes before writing the final implementation for the <code>MoveNext</code> method. First, let&#8217;s add some more needed properties:</p>
<ul>
<li><b>maxPostalAddresses As Integer</b>. This will be responsible for storing the quantity of elements available under the PostalAddress Array.</li>
<li><b>source As Person</b>. This will be in charge of saving the reference to the Person object it has to iterate.</li>
<li>The Calculated Property <b>valid as Boolean</b>. We will use the <code>Get</code> method from this calculated property to know if the PostalAddress Array has been modified during the iteration of the object, returning <code>False</code> when this is the case. In fact, this is the code for the <code>Get</code><br />
method:</li>
</ul>
<pre>Return if(source.postalAddresses.Ubound = maxPostalAddresses, True, false)</pre>
<p>Next, we will add the <code>Constructor</code> method to the class using this signature:</p>
<pre>Constructor( value As Person )</pre>
<p>and the following code for the method:</p>
<pre>source = value
maxPostalAddresses = value.postalAddresses.Ubound</pre>
<p>That is: we initialize the previously declared property&#8217;s values with the reference to the Person object that we will use to iterate over, and the quantity of elements included in the Array when we initialize the class; thus, the original items quantity that we will use to check if it has been modified during the iteration process.</p>
<p>Now we can move back to the <code>MoveNext</code> method to write its final implementation:</p>
<pre>If valid Then
  counter = counter + 1
  Return If(counter &lt;= source.postalAddresses.Ubound, True, False)
Else
  Dim e As New Xojo.Core.IteratorException
  e.Reason = "Invalid Data"
  Raise e
End If</pre>
<p>Inside the method implementation we will check also if the object has been modified during the iteration, returning the corresponding value:</p>
<pre>If valid Then
  Return source.postalAddresses(counter)
Else
  Dim e As New Xojo.Core.IteratorException
  e.Reason = "Invalid Data"
  Raise e
End If</pre>
<p>As you can see, the Value method will return the PostalAddress item from the Person array, using as Index the value of the Counter property.</p>
<h2>Creating the Iterable Class</h2>
<p>Now we have to create the iterable class itself. For that we will add a new class to the project using<code>IterablePerson</code> as its name and making it a subclass from the Person class, so we have to write this data type in the <code>Super</code> field of the Inspector Panel. In addition, this class will be the one that implements the class interface <a href="http://developer.xojo.com/xojo-core-iterable"><b>Xojo.Core.Iterable</b></a>, so you have to push on the <code>Choose</code> button associated with the <code>Interfaces</code> label in order to select and confirm the addition of the corresponding methods. In fact, is just one method:</p>
<pre>GetIterator() As Xojo.Core.Iterator</pre>
<p>And this is the code you have to write for that method:</p>
<pre>Dim it As New iterablePerson(Self)
Return it</pre>
<p>With this we have all the definitions we need for the subclass: we get a new <code>IterablePerson</code> instance passing as parameter the iterable object (a Person subclass). That&#8217;s all! Finally, we return the iterator we get and that will be used inside the <code>For Each… Next</code> loop.</p>
<h2>Putting it all together</h2>
<p>Armed with all the needed classes for our example, we can finally put it all into practice. For this, we will use a <a href="http://developer.xojo.com/listbox">ListBox</a> in the main project Window (<code>Window1</code>). This is the control where we will add as many rows as postal addresses associated with a instance of the Person class used for the example. For this, add also a button to the window with the Action event and write the following code in the resulting Code Editor:</p>
<pre>Dim p As New iterablePerson
p.name = "Juan"
p.middleName = "Sin Miedo"
p.secondName = "De Nada"
Dim d As New PostalAddress
d.street = "Juan de Urbieta"
d.streetNumber = 6
d.streetFlat = 1
p.postalAddresses.Append d
d = new PostalAddress
d.street = "Mi calle bonita"
d.streetNumber = 8
d.streetFlat = 3
p.postalAddresses.Append d
d = new PostalAddress
d.street = "Princesa"
d.streetNumber = 5
d.streetFlat = 5
p.postalAddresses.Append d
For Each s As PostalAddress In p
  Listbox1.AddRow s.street + ", " + s.streetNumber.ToText + " Piso: " + s.streetFlat.ToText
Next</pre>
<p>Run the project, push the button and you will see how the <code>For Each… Next</code> loop iterates every postal address associated with the Person object. This is a more concise, stylish and OOP way of doing things when compared with the classic <code>For… Next</code> loop, where we have to get the upper value (or limit) and use a variable as index to get every collection object we are interested in.</p>
<p>For more on this topic:</p>
<ul>
<li><a href="http://developer.xojo.com/webinar-arrays-dictionaries-iterators">Video: Arrays, Dictionaries, Iterators</a></li>
<li><a href="http://developer.xojo.com/userguide/iterator-and-iterable-interfaces">User Guide: Iterator and Iterable Interfaces</a></li>
</ul>
<p><em>Javier Rodri­guez has been&nbsp;the Xojo Spanish&nbsp;Evangelist since 2008, he’s also a Developer, Consultant and Trainer who&nbsp;has be using&nbsp;Xojo since 1998. He manages&nbsp;<a href="http://www.aprendexojo.com">AprendeXojo.com</a> and is the developer behind the GuancheMOS plug-in for Xojo Developers and the Snippery app, among others.</em></p>
<p>*<a href="https://www.aprendexojo.com/2017/07/implementa-clases-iterables-en-xojo/">Read this post in Spanish</a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
