<?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>Method Overload &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/method-overload/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Mon, 04 May 2026 15:32:06 +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>Build a Recursive Find in Files Function</title>
		<link>https://blog.xojo.com/2026/05/04/build-a-recursive-find-in-files-function/</link>
		
		<dc:creator><![CDATA[Gabriel Ludosanu]]></dc:creator>
		<pubDate>Mon, 04 May 2026 15:32:02 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[Method Overload]]></category>
		<category><![CDATA[Recursiveness]]></category>
		<category><![CDATA[Search]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=16179</guid>

					<description><![CDATA[In the&#160;previous post, we built a utility to find text inside files within a single folder. Today we&#8217;re upgrading that utility to support recursive searching&#8230;]]></description>
										<content:encoded><![CDATA[
<p>In the&nbsp;<a href="https://blog.xojo.com/2026/04/02/build-a-simple-find-in-files-function/">previous post</a>, we built a utility to find text inside files within a single folder. Today we&#8217;re upgrading that utility to support recursive searching with depth control. We&#8217;ll also refactor the code to keep it maintainable as it gets more complex.</p>



<h2 class="wp-block-heading" id="the-goal">The Goal</h2>



<p>We want to:</p>



<ol class="wp-block-list">
<li>Search through subfolders automatically.</li>



<li>Add a&nbsp;<code>maxDepth</code>&nbsp;parameter so we don&#8217;t accidentally crawl the entire hard drive.</li>



<li>Keep the simple version working using method overloading.</li>
</ol>



<h2 class="wp-block-heading" id="step-1-move-file-reading-to-its-own-method">Step 1: Move File Reading to its own Method</h2>



<p>The original&nbsp;<code>FindInFiles</code>&nbsp;function did everything: validated the folder, looped through children, read file content, and matched text. Adding recursion to that would make the code look ugly, so let&#8217;s refactor our code.</p>



<p>We&#8217;ll start by moving the file-reading logic into its own private method:&nbsp;<code>SearchFile</code>. This keeps the &#8220;search&#8221; logic separate from the &#8220;looping&#8221; logic.</p>



<pre class="wp-block-code"><code>Private Sub SearchFile(file As FolderItem, searchTerm As String, hits() As SearchHit)
  Try
    Var input As TextInputStream = TextInputStream.Open(file)
    input.Encoding = Encodings.UTF8
    Var lineNumber As Integer = 0
    While Not input.EndOfFile
      Var line As String = input.ReadLine
      lineNumber = lineNumber + 1
      If line.Contains(searchTerm, ComparisonOptions.CaseInsensitive) Then
        hits.Add(New SearchHit(file.NativePath, lineNumber, line))
      End If
    Wend
    input.Close
  Catch error As IOException
    // Skip unreadable files
  End Try
End Sub</code></pre>



<h2 class="wp-block-heading" id="step-2-recursive-folder-traversal">Step 2: Recursive Folder Traversal</h2>



<p>Now we create&nbsp;<code>SearchFolder</code>. This method loops through a folder. If it finds a file, it calls&nbsp;<code>SearchFile</code>. If it finds a folder, it calls itself.</p>



<p>To keep this safe, we Use&nbsp;<code>maxDepth</code>:</p>



<ul class="wp-block-list">
<li><code>maxDepth = 0</code>: Search only this folder.</li>



<li><code>maxDepth &gt; 0</code>: Search this folder and N levels of subfolders.</li>



<li><code>maxDepth = -1</code>: Search everything (unlimited).</li>
</ul>



<pre class="wp-block-code"><code>Private Sub SearchFolder(folder As FolderItem, searchTerm As String, hits() As SearchHit, maxDepth As Integer)
   Try
    For Each item As FolderItem In folder.Children
      If item Is Nil Or Not item.Exists Or Not item.IsReadable Then Continue
      If item.IsFolder Then
        If maxDepth &lt;&gt; 0 Then
          Var nextDepth As Integer = If(maxDepth &gt; 0, maxDepth - 1, maxDepth)
          SearchFolder(item, searchTerm, hits, nextDepth)
        End If
      Else
        SearchFile(item, searchTerm, hits)
      End If
    Next
  Catch error As IOException
    // Skip inaccessible folders
  End Try
End Sub</code></pre>



<p>The logic in&nbsp;<code>nextDepth</code>&nbsp;lets us count down if there&#8217;s a limit, or stay at -1 if there isn&#8217;t.</p>



<h2 class="wp-block-heading" id="step-3-support-both-versions-with-method-overloading">Step 3: Support Both Versions with Method Overloading</h2>



<p>Overloading is a feature that lets you have multiple methods with the same name, as long as they have different parameters. In our case, it allows us to provide a simple version for one folder, and a recursive version with a depth limit. Xojo will automatically pick the right one based on the arguments you pass. You can read more about it in the&nbsp;<a href="https://documentation.xojo.com/getting_started/object-oriented_programming/oop_design_concepts.html#getting-started-object-oriented-programming-oop-design-concepts-overloading" target="_blank" rel="noreferrer noopener">Xojo Documentation</a>.</p>



<p>The default version (depth 0):</p>



<pre class="wp-block-code"><code>Public Function FindInFiles(targetFolder As FolderItem, searchTerm As String) As SearchHit()
  Return FindInFiles(targetFolder, searchTerm, 0)
End Function</code></pre>



<p>The recursive version:</p>



<pre class="wp-block-code"><code>Public Function FindInFiles(targetFolder As FolderItem, searchTerm As String, maxDepth As Integer) As SearchHit()
  Var hits() As SearchHit
  If targetFolder Is Nil Or Not targetFolder.Exists Or Not targetFolder.IsFolder Or searchTerm.IsEmpty Then
    Return hits
  End If
  Try
    SearchFolder(targetFolder, searchTerm, hits, maxDepth)
  Catch error As IOException
    // Skip inaccessible folders
  End Try
  Return hits
End Function</code></pre>



<p id="why-this-structure">By separating the validation, traversal, and processing, the code is much easier to modify. If you want to add Regex support, you only change&nbsp;<code>SearchFile</code>. If you want to filter by file extension, you only change&nbsp;<code>SearchFolder</code>.</p>



<h2 class="wp-block-heading" id="summary">Summary</h2>



<p>Now, you can search a single folder just like before, or crawl a directory tree with one extra parameter:</p>



<pre class="wp-block-code"><code>// Search up to 3 levels deep
Var results() As SearchUtils.SearchHit = SearchUtils.FindInFiles(myFolder, "TODO", 3)</code></pre>



<h2 class="wp-block-heading" id="the-complete-recursive-code">The Complete Recursive Code</h2>



<h3 class="wp-block-heading" id="the-searchutils-module">The SearchUtils Module</h3>



<pre class="wp-block-code"><code>Module SearchUtils

  Class SearchHit
    Public Property FilePath As String
    Public Property LineNumber As Integer
    Public Property LineText As String

    Public Sub Constructor(filePath As String, lineNumber As Integer, lineText As String)
      Self.FilePath = filePath
      Self.LineNumber = lineNumber
      Self.LineText = lineText
    End Sub
  End Class

  Public Function FindInFiles(targetFolder As FolderItem, searchTerm As String) As SearchHit()
    // Simple usage: depth = 0
    Return FindInFiles(targetFolder, searchTerm, 0)
  End Function

  Public Function FindInFiles(targetFolder As FolderItem, searchTerm As String, maxDepth As Integer) As SearchHit()
    // Recursive usage: maxDepth ( -1 is unlimited )

    Var hits() As SearchHit

    If targetFolder Is Nil Or Not targetFolder.Exists Or Not targetFolder.IsFolder Or searchTerm.IsEmpty Then
      Return hits
    End If

    Try
      SearchFolder(targetFolder, searchTerm, hits, maxDepth)
    Catch error As IOException
      // Skip inaccessible folders
    End Try

    Return hits
  End Function

  Private Sub SearchFolder(folder As FolderItem, searchTerm As String, hits() As SearchHit, maxDepth As Integer)
    Try
      For Each item As FolderItem In folder.Children
        If item Is Nil Or Not item.Exists Or Not item.IsReadable Then Continue

        If item.IsFolder Then
          If maxDepth &lt;&gt; 0 Then
            Var nextDepth As Integer = If(maxDepth &gt; 0, maxDepth - 1, maxDepth)
            SearchFolder(item, searchTerm, hits, nextDepth)
          End If
        Else
          SearchFile(item, searchTerm, hits)
        End If
      Next
    Catch error As IOException
      // Skip inaccessible folders
    End Try
  End Sub

  Private Sub SearchFile(file As FolderItem, searchTerm As String, hits() As SearchHit)
    Try
      Var input As TextInputStream = TextInputStream.Open(file)
      input.Encoding = Encodings.UTF8
      Var lineNumber As Integer = 0

      While Not input.EndOfFile
        Var line As String = input.ReadLine
        lineNumber = lineNumber + 1

        If line.Contains(searchTerm, ComparisonOptions.CaseInsensitive) Then
          hits.Add(New SearchHit(file.NativePath, lineNumber, line))
        End If
      Wend

      input.Close
    Catch error As IOException
      // Skip unreadable files
    End Try
  End Sub
End Module</code></pre>



<p>Happy coding!</p>



<p><em>Gabriel is a digital marketing enthusiast who loves coding with Xojo to create cool software tools for any platform. He is always eager to learn and share new ideas!</em></p>



<ul class="wp-block-social-links has-normal-icon-size is-content-justification-center is-layout-flex wp-container-core-social-links-is-layout-16018d1d wp-block-social-links-is-layout-flex"><li class="wp-social-link wp-social-link-facebook  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.facebook.com/goxojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"></path></svg><span class="wp-block-social-link-label screen-reader-text">Facebook</span></a></li>

<li class="wp-social-link wp-social-link-x  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://x.com/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z" /></svg><span class="wp-block-social-link-label screen-reader-text">X</span></a></li>

<li class="wp-social-link wp-social-link-linkedin  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.linkedin.com/company/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M19.7,3H4.3C3.582,3,3,3.582,3,4.3v15.4C3,20.418,3.582,21,4.3,21h15.4c0.718,0,1.3-0.582,1.3-1.3V4.3 C21,3.582,20.418,3,19.7,3z M8.339,18.338H5.667v-8.59h2.672V18.338z M7.004,8.574c-0.857,0-1.549-0.694-1.549-1.548 c0-0.855,0.691-1.548,1.549-1.548c0.854,0,1.547,0.694,1.547,1.548C8.551,7.881,7.858,8.574,7.004,8.574z M18.339,18.338h-2.669 v-4.177c0-0.996-0.017-2.278-1.387-2.278c-1.389,0-1.601,1.086-1.601,2.206v4.249h-2.667v-8.59h2.559v1.174h0.037 c0.356-0.675,1.227-1.387,2.526-1.387c2.703,0,3.203,1.779,3.203,4.092V18.338z"></path></svg><span class="wp-block-social-link-label screen-reader-text">LinkedIn</span></a></li>

<li class="wp-social-link wp-social-link-github  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://github.com/topics/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z"></path></svg><span class="wp-block-social-link-label screen-reader-text">GitHub</span></a></li>

<li class="wp-social-link wp-social-link-youtube  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.youtube.com/c/XojoInc" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M21.8,8.001c0,0-0.195-1.378-0.795-1.985c-0.76-0.797-1.613-0.801-2.004-0.847c-2.799-0.202-6.997-0.202-6.997-0.202 h-0.009c0,0-4.198,0-6.997,0.202C4.608,5.216,3.756,5.22,2.995,6.016C2.395,6.623,2.2,8.001,2.2,8.001S2,9.62,2,11.238v1.517 c0,1.618,0.2,3.237,0.2,3.237s0.195,1.378,0.795,1.985c0.761,0.797,1.76,0.771,2.205,0.855c1.6,0.153,6.8,0.201,6.8,0.201 s4.203-0.006,7.001-0.209c0.391-0.047,1.243-0.051,2.004-0.847c0.6-0.607,0.795-1.985,0.795-1.985s0.2-1.618,0.2-3.237v-1.517 C22,9.62,21.8,8.001,21.8,8.001z M9.935,14.594l-0.001-5.62l5.404,2.82L9.935,14.594z"></path></svg><span class="wp-block-social-link-label screen-reader-text">YouTube</span></a></li></ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Creating a Number Class</title>
		<link>https://blog.xojo.com/2023/06/13/creating-a-number-class/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Tue, 13 Jun 2023 13:25:48 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Beginner Tips]]></category>
		<category><![CDATA[Constructor]]></category>
		<category><![CDATA[Method Overload]]></category>
		<category><![CDATA[Object-Oriented]]></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=11585</guid>

					<description><![CDATA[In this tutorial I will show you how to create a Number class that can tell you when its value has been actually set, along with other methods to manage how it is used while demonstrating some common object-oriented techniques and features such as operator overloading. You'll be able to create a Number from an Integer, Double or String.]]></description>
										<content:encoded><![CDATA[
<p>In this tutorial I will show you how to create a Number class that can tell you when its value has been actually set, along with other methods to manage how it is used while demonstrating some common object-oriented techniques and features such as operator overloading. You&#8217;ll be able to create a Number from an Integer, Double or String.</p>



<span id="more-11585"></span>



<h3 class="wp-block-heading">The Basics of the Number Class</h3>



<p>Let&#8217;s start with the bare bones of this Number class. Add a new Class to a Xojo project and name it Number in the Inspector Panel.</p>



<p>The value of the Number class is going to be backed by a primitive double property. So add a new Property to the Number class and use the Inspector Panel with the following values:</p>



<ul class="wp-block-list">
<li><strong>Name:</strong> Value</li>



<li><strong>Type:</strong> Double</li>



<li><strong>Scope:</strong> Protected</li>
</ul>



<p>We also want to know when a value is set to any of the Number instances, so go ahead and add a new Property to the Class using the following values in the Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Name:</strong> IsSet</li>



<li><strong>Type:</strong> Boolean</li>



<li><strong>Scope:</strong> Protected</li>
</ul>



<p>And of course we want to have the ability to enable or disable any Number instance, so its assigned value may or may not be taken into account when used in any mathematical operation, for example. Add the third of the properties required by our Number class:</p>



<ul class="wp-block-list">
<li><strong>Name:</strong> Enabled</li>



<li><strong>Type:</strong> Boolean</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>The next step is to add some constructor Methods to our class, so we can create new instances of it covering several common cases:</p>



<ul class="wp-block-list">
<li>Creating a new instance with no value provided: IsSet property will be set to False, and the Enabled property will be set to True.</li>



<li>Creating a new instance with the Integer/Double provided: IsSet property will be set to True, and the Enabled property will be set to True.</li>



<li>Creating a new instance with the numeric value from the provided String: IsSet property will be set to True and the Enabled property will be set to True too.</li>
</ul>



<p>The default constructor for the class will be the one with no parameters. So add a new method to the Number class using the following values in the Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Constructor</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type the following line of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Self.Enabled = True</pre>



<p>Add a second method to the class and use the following values in the Inspector Panel (this constructor will create a new instance from the received Integer/Double value):</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Constructor</li>



<li><strong>Parameters:</strong> value As Double</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type the following lines of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Self.Constructor
Self.Value = value
Self.IsSet = True</pre>



<p>Let&#8217;s add the third constructor, the one taking a String as the parameter. Add a third method to the class using the following values in the Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Constructor</li>



<li><strong>Parameters:</strong> value As String</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type the following lines of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Self.Constructor

Self.Value = Double.FromString(value, Locale.Raw)
Self.IsSet = True</pre>



<p>But, wait! Wouldn&#8217;t it be great if we could create new Number instances from another Number instance received as the parameter? Of course it would! So add a new Method again using the following values in the Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Constructor</li>



<li><strong>Parameters:</strong> value As Number</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type the following lines of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Self.Constructor

If value &lt;&gt; Nil and value.IsSet And value.Enabled Then
  Self.Value = value.Value
  Self.IsSet = True
End If</pre>



<p>By now our class has four special methods, all of them with the same name: Constructor. If defined in any of your classes, this is the method that will be called/executed every time your code uses the New keyword in combination with the class data type. For example:</p>



<pre id="Xojo" class="wp-block-preformatted">Var n As New Number // This calls the by default Constructor.
Var i As New Number(10) // This calls the Constructor taking an Integer/Double as the parameter.
Var s As New Number("120.20") // This calls the Constructor taking a String as the parameter.
Var objNumber As New Number(n) // This calls the Constructor taking a Number instance as the parameter.</pre>



<p>When several methods are added to a class using the same method name but with different number of arguments or arguments data types (or returned values), that is called <strong>Method Overloading</strong> and you will find it in any object-oriented programming language. So our constructor method is overloaded to cover all the expected cases.</p>



<p>As you see, there is nothing fancy in the previous examples. I mean, creating a new instance from the received value in the constructor method is ok, but it would be even better and more simple if we could do something like:</p>



<pre id="Xojo" class="wp-block-preformatted">Var i As Number = 10
Var s As Number = "120.20"
Var objNumber = n</pre>



<h3 class="wp-block-heading">Overloading the Assignment Operator</h3>



<p>The good news is that we can do that! The Xojo programming language has the ability to overload operators too, just like we did with the constructor method! And when it is about to overload the assignment operator we implement the <a href="https://documentation.xojo.com/api/math/operator_convert.html#operator-convert">Operator_Convert</a> method. This one will be called every time our class receives a value from another data type or needs to convert itself to the expected data type; for example:</p>



<pre id="xojo" class="wp-block-preformatted">Var i As Number = 10 // 10 Integer value needs to be converted to a Number instance.
Var intValue As Integer = i // The variable "i" (Number data type) needs to be converted to an Integer data type.</pre>



<p>The most important thing to remember is that when Operator_Convert gets into play, the class Constructor is never called! So the code in the Operator_Convert method implementation needs to take care of properly initialising the instance as you expect.</p>



<p>So let&#8217;s start implementing the Operator_Convert that will be called every time a Number instance needs to be converted to an Integer or Double value.</p>



<p>Add a new method to the Number class using the following values:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_Convert</li>



<li><strong>Return Type:</strong> Double</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type the following line of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Return If(Self.Enabled, Self.Value, 0)</pre>



<p>Add a second Operator_Convert method, this time change the returned value to a String so our Number instances internal values can be easily assigned to a String variable (or used in other operations with Strings) like this:</p>



<pre id="Xojo" class="wp-block-preformatted">Var i As Number = 10
Var s As String = i</pre>



<p>Use the following values in the associated Inspector Panel and type the line of code in the associated Code Editor:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_Convert</li>



<li><strong>Return Type:</strong> String</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">Return If(Self.Enabled, Self.Value.ToString(Locale.Raw), "")</pre>



<p>We need to overload the Operator_Convert method two more times so it can handle the assignment operation when the received value is at the right of the equal operator instead of the left.</p>



<p>Use the following values in the Inspector Panel for every one of the new two overloaded methods (typing the line(s) of code following each of these):</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_Convert</li>



<li><strong>Parameters:</strong> value As Double</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">Self.Enabled = True
Self.Value = value
Self.IsSet = True</pre>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_Convert</li>



<li><strong>Parameters:</strong> value As String</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">Self.Enabled = True
Self.Value = Double.FromString(value, locale.Raw)
Self.IsSet = True</pre>



<h3 class="wp-block-heading">Overloading the Add Operator</h3>



<p>Assigning values to Number variables or properties or Number instances to Integer / Double or String variables and properties is fine, but not so useful now. So the next step is adding Number instances with another Number instances, Integer / Double data types and also even with all the values from an array. To do this we need to overload the &#8220;+&#8221; (Add) operator implementing the <a href="https://documentation.xojo.com/api/math/operator_add.html#operator-add">Operator_Add</a> method in the Number class.</p>



<p>This will allow us to write code like this:</p>



<pre id="Xojo" class="wp-block-preformatted">Var n2 As Number = i + 10</pre>



<p>Here the &#8220;10&#8221; value is first converted to a Number instance that is going to be added to the &#8220;i&#8221; Number instance and then assigned to the &#8220;n2&#8221; variable.</p>



<pre id="Xojo" class="wp-block-preformatted">Var n3 As Integer = i + 10</pre>



<p>Here the &#8220;10&#8221; value is first converted to a Number instance that is added to the &#8220;i&#8221; number instance and, then, the result converted to an integer value assigned to the &#8220;n3&#8221; variable.</p>



<p>So we need to implement the Operator_Add taking a Number parameter and returning also a Number data type.</p>



<p>Add a new method to the Number class using the following values in the Inspector Panel, and typing the lines of code following them in the associated Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_Add</li>



<li><strong>Parameters:</strong> Value As Number</li>



<li><strong>Return Type:</strong> Number</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">If value &lt;&gt; Nil Then
  Var no As New Number(If(Self.Enabled, Self.Value, 0) + value.Value)
  Return no
End If</pre>



<p>That one works well when we have a Number instance to the left of the add operation, but not when the Number instance is to the right. For that scenarios we need to overload another method: <a href="https://documentation.xojo.com/api/math/operator_addright.html">Operator_AddRight</a>.</p>



<p>Add another method to the Number class using the following values in the associated Inspector Panel:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_AddRight</li>



<li><strong>Parameters:</strong> value As Double</li>



<li><strong>Return Type:</strong> Double</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And type this line of code in the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Return If(Self.Enabled, Self.Value, 0) + value</pre>



<h3 class="wp-block-heading">Adding Number Instances to an Array of Integers</h3>



<p>It would be also really helpful if we could add a Number to an array of integer values or even a Number instance to an array containing other array instances. For example, it would look like this in code:</p>



<pre id="Xojo" class="wp-block-preformatted">Var arrayNumbers() As Number
Var arrayIntegers() As Integer

For z As Integer = 0 To 10
  arrayNumbers.Add(System.Random.InRange(10, 200))
  arrayIntegers.Add(System.Random.InRange(10, 200))
Next

Var z1 As Number = i + arrayNumbers
Var z3 As Integer = i + arrayIntegers</pre>



<p>Of course this implies implementing new overloaded methods both for Convert_Add and Convert_AddRight. These are the implementations for overloading Convert_Add taking an Array of Doubles and also taking an Array of Number instances. I will let the Convert_AddRight implementation to you as exercice:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_AddRight</li>



<li><strong>Parameters:</strong> value() As Integer</li>



<li><strong>Return Type:</strong> Double</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">Var sum As Double

For Each item As Double In values
  sum = sum + item
Next

Return sum + If(Self.Enabled, Self.Value, 0)</pre>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Operator_AddRight</li>



<li><strong>Parameters:</strong> value() As Number</li>



<li><strong>Return Type:</strong> Number</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<pre id="Xojo" class="wp-block-preformatted">Var sum As Double

For Each item As Number In values
  If item &lt;&gt; Nil And item.Enabled Then sum = sum + item.Value
Next

Return sum + If(Self.Enabled, Self.Value, 0)</pre>



<h3 class="wp-block-heading">Unset Number Instances</h3>



<p>The last step in this Number class is to implement the Method that will take care of Unset the instance; that is, kind of &#8220;reset&#8221; state where the value is still not defined.</p>



<p>Add a new Method to the Number class using the following values:</p>



<ul class="wp-block-list">
<li><strong>Method Name:</strong> Unset</li>



<li><strong>Scope:</strong> Public</li>
</ul>



<p>And adding the following lines of code to the associated Code Editor:</p>



<pre id="Xojo" class="wp-block-preformatted">Self.IsSet = False
Self.Value = 0</pre>



<h3 class="wp-block-heading">Conclusion</h3>



<p>As you see, method overloading is a powerful technique as we have done with the constructor method and also with the Operator_Convert, Operator_Add and Operator_AddRight methods. This Number class, with a little work, can be made to to cover other scenarios like the subtract, multiply or divide operations, among others. That wouldn&#8217;t be too difficult based on the implementations already exposed for the overloaded Operator_Add and Operator_AddRight methods. <a href="https://drive.google.com/file/d/1DfRL8WofvmdIp6dM__s_fmnswO4cx6-5/view?usp=share_link">Download</a> the Numbers class project.</p>



<p>Further Reading from the Xojo Blog Archives</p>



<p><a href="https://blog.xojo.com/2016/05/25/methods-overloading-computed-properties-setters-and-getters/">Methods Overloading: Computed Properties, Setters and Getters</a></p>



<p>More on Object-Oriented Programming from the <a href="https://blog.xojo.com/tag/object-oriented/">Xojo Blog</a> and <a href="https://documentation.xojo.com/getting_started/object-oriented_programming/index.html">Xojo Documentation</a></p>



<p><em>Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón</em>, <em>Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at <a href="https://twitter.com/xojoes" target="_blank" rel="noreferrer noopener">@XojoES</a> or on the <a href="https://forum.xojo.com/u/javier_menendez/summary" target="_blank" rel="noreferrer noopener">Xojo Forum</a>.</em></p>



<ul class="wp-block-social-links has-normal-icon-size is-content-justification-center is-layout-flex wp-container-core-social-links-is-layout-16018d1d wp-block-social-links-is-layout-flex"><li class="wp-social-link wp-social-link-facebook  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.facebook.com/goxojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"></path></svg><span class="wp-block-social-link-label screen-reader-text">Facebook</span></a></li>

<li class="wp-social-link wp-social-link-x  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://x.com/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z" /></svg><span class="wp-block-social-link-label screen-reader-text">X</span></a></li>

<li class="wp-social-link wp-social-link-linkedin  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.linkedin.com/company/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M19.7,3H4.3C3.582,3,3,3.582,3,4.3v15.4C3,20.418,3.582,21,4.3,21h15.4c0.718,0,1.3-0.582,1.3-1.3V4.3 C21,3.582,20.418,3,19.7,3z M8.339,18.338H5.667v-8.59h2.672V18.338z M7.004,8.574c-0.857,0-1.549-0.694-1.549-1.548 c0-0.855,0.691-1.548,1.549-1.548c0.854,0,1.547,0.694,1.547,1.548C8.551,7.881,7.858,8.574,7.004,8.574z M18.339,18.338h-2.669 v-4.177c0-0.996-0.017-2.278-1.387-2.278c-1.389,0-1.601,1.086-1.601,2.206v4.249h-2.667v-8.59h2.559v1.174h0.037 c0.356-0.675,1.227-1.387,2.526-1.387c2.703,0,3.203,1.779,3.203,4.092V18.338z"></path></svg><span class="wp-block-social-link-label screen-reader-text">LinkedIn</span></a></li>

<li class="wp-social-link wp-social-link-github  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://github.com/topics/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z"></path></svg><span class="wp-block-social-link-label screen-reader-text">GitHub</span></a></li>

<li class="wp-social-link wp-social-link-youtube  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.youtube.com/c/XojoInc" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M21.8,8.001c0,0-0.195-1.378-0.795-1.985c-0.76-0.797-1.613-0.801-2.004-0.847c-2.799-0.202-6.997-0.202-6.997-0.202 h-0.009c0,0-4.198,0-6.997,0.202C4.608,5.216,3.756,5.22,2.995,6.016C2.395,6.623,2.2,8.001,2.2,8.001S2,9.62,2,11.238v1.517 c0,1.618,0.2,3.237,0.2,3.237s0.195,1.378,0.795,1.985c0.761,0.797,1.76,0.771,2.205,0.855c1.6,0.153,6.8,0.201,6.8,0.201 s4.203-0.006,7.001-0.209c0.391-0.047,1.243-0.051,2.004-0.847c0.6-0.607,0.795-1.985,0.795-1.985s0.2-1.618,0.2-3.237v-1.517 C22,9.62,21.8,8.001,21.8,8.001z M9.935,14.594l-0.001-5.62l5.404,2.82L9.935,14.594z"></path></svg><span class="wp-block-social-link-label screen-reader-text">YouTube</span></a></li></ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Create a Preferences Class with Operator_Lookup</title>
		<link>https://blog.xojo.com/2018/06/20/create-a-preferences-class-with-operator_lookup/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Wed, 20 Jun 2018 10:00:58 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Constructor]]></category>
		<category><![CDATA[Method Overload]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Singleton]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=4381</guid>

					<description><![CDATA[Create a Preferences Class with Operator_Lookup: We have at our disposal an operator we can overload: Lookup. Let's explore those advantages and features while building a Preferences class we can use in any of our projects.]]></description>
										<content:encoded><![CDATA[<p>Xojo is an <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">Object Oriented Programming Language</a> and, among other things, that means that it supports <a href="https://blog.xojo.com/2016/05/25/methods-overloading-computed-properties-setters-and-getters/">Methods Overloading</a>. We have seen in other posts that some of these overloaded methods can be Class Constructors, but, there are others things you can do. For example, we can overload the operators. These are the methods in charge of adding two instances of the same class, subtracting, multiplying or dividing them. But we also have at our disposal another operator we can overload: <b>Lookup</b>. What advantages does this give us and how it does it work? Let&#8217;s explore it while building a Preferences class we can use in any of our projects.<span id="more-4381"></span></p>
<p>When we define and implement the <b>Operator_Lookup</b> method in our classes, we will be able to use the dot notation to access and assign a value to a class member that has not been defined for the class! That is, it will be the code implemented in this method that will decide how it will act, not the Xojo Compiler. Among other possibilities, this will bring us the flexibility to create a Preferences class that is not attached to a particular project, and that is multiplatform, of course, backed by as many pairs of Keys/Values in a Dictionary as we need to store our apps preferences. This way we can use the simple dot notation both to store and assign the stored values. Let&#8217;s put this into practice.</p>
<p>The first step is adding a new Class item to the project without assigning a Super class; that is, this will be a base class. For this example, we will use <code>MyPreferences</code> as the name of the class. Then, we will add a property with <code>Data</code> as the Name and <code>Xojo.Core.Dictionary</code> as the data type, setting its scope to Private (this means that the property will be accessible only from the own class). This property will act as the storage for the Key/Value pairs used for our Preference Class.</p>
<p>Add now a new <b>Constructor</b> method in charge of initializing the just defined Dictionary property:</p>
<ul>
<li><b>Name</b>: Constructor</li>
<li><b>Scope</b>: Public</li>
</ul>
<p>And put this line of code in the Code Editor for the Constructor method:</p>
<pre>Data = New Xojo.Core.Dictionary</pre>
<p>Now we will add the method that we are really interested in. This is, the overload for the Operator_Lookup method:</p>
<ul>
<li><b>Name</b>: Operator_Lookup</li>
<li><b>Parameters</b>: key as string, assigns value as auto</li>
<li><b>Scope</b>: Public</li>
</ul>
<p>Writing the following line of code in the resulting Code Editor:</p>
<pre>data.Value( key ) = value</pre>
<p>Once implemented, we can take advantage of the feature. If we add the Open Event to the project and write the below snippet of code, we will see how we can use the dot notation to use and access class members that have never been added to the class. These members are <em>captured</em> by the <code>Operator_Lookup</code> method, adding the member as the key for a new entry in the dictionary (this is the text we write after the dot) and assigning to it the value we put after the equal symbol (the assignation operator); thanks to the use of the <code>assigns</code> keyword in the parameters declaration for the <code>Operator_Lookup</code> method.</p>
<pre>Dim pr As New MyPreferences
pr.page = 10
pr.index = "20"
pr.baseColor = &amp;c204060</pre>
<p>As you can see, <code>Page</code>, <code>Index</code> and <code>BaseColor</code> are not members of our class; these are captured as Keys for the Dictionary thanks to the Operator_Lookup operator. In addition, you can see how we can assign several types of values to these: an Integer, a String and a Color value.</p>
<h2>Retrieving data with Operator_Lookup</h2>
<p>Now, how can we retrieve values for these not existing members using the dot notation? As you probably guessed, we need to add an additional method that overloads Operator_Lookup. In this case the method signature will accept as the parameter the Key we want to retrieve (the class member put after the dot), returning always a String, in order to simplify this example. Thus, add a new method for the class using the following signature:</p>
<ul>
<li><b>Name</b>: Operator_Lookup</li>
<li><b>Parameters</b>: Key As String</li>
<li><b>Return Type</b>: String</li>
<li><b>Scope</b>: Public</li>
</ul>
<p>Writing the following code in the associated Code Editor:</p>
<pre>Dim a As Auto
Dim s As String
If data.HasKey(key) Then
  a = data.Value(key)
  Dim info As Xojo.Introspection.TypeInfo
  info = xojo.Introspection.GetType(a)
  If info = GetTypeInfo(Text) Or info = GetTypeInfo(String) Then
    s = a
  Elseif info = GetTypeInfo(Integer) Then
    s = CType(a, Integer).totext
  Elseif info= GetTypeInfo(Color) Then
    Dim c As Color = a
    s = Str(c)
  End If
End If
Return s</pre>
<p>As you can see, and after verifying that the Dictionary has the Key, we use the <b>Introspection</b> mechanisms provided by the language in order to see what type is associated with the key. Once we have its type, we will convert the value to a String using the appropriate approach for each case: an Integer, a Text/String or a Color. Of course, for brevity, this example doesn&#8217;t take into account other possible value types; but that is something you can implement yourself!</p>
<p>Let&#8217;s go back to the Open Event for the project and add the following line of code at the end:</p>
<p><code>MsgBox pr.page + EndOfLine + pr.index  + EndOfLine + pr.baseColor</code></p>
<p>Run it and observe how we can assign values and retrieve them (always as a String) simply using the dot notation. We can take this class from one project to other and use the dot notation to reference the elements we want to save and retrieve as preferences.</p>
<p><iframe title="Obtén la máxima flexibilidad con Operator_Lookup" width="500" height="375" src="https://www.youtube.com/embed/Tya8tbi9xIM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<h2>Final Word</h2>
<p>If you decide to use this approach for your projects, there are some considerations you have to take into account. For example, you will not be able to take advantage of the Autocompletion feature provided by the IDE. In addition, we will have to pay special attention to write exactly the same &#8220;member&#8221; in all the cases we want refer to it; otherwise we will be using a different key with unexpected results. After all, once implemented, Operator_Lookup for the class the Compiler will not complain about the undefined members you are trying to access!</p>
<p>It would also be very easy to expand the class so it can work with other data types; adding an additional method to serialize the Key/Values pairs so they can be stored on file/database for a later retrieval and reconstruction via an additional Constructor. Finally, it is very probable that you only want to use a MyPreferences instance in your app, so you could implement this class as a <a href="https://blog.xojo.com/2016/06/08/design-patterns-in-xojo-singleton/">Singleton</a>. Do you dare? If you do, <a href="https://twitter.com/AprendeXojo">tell me about it</a>!</p>
<p><em>Javier Rodri­guez has been the Xojo Spanish Evangelist since 2008, he’s also a Developer, Consultant and Trainer who has be using Xojo since 1998. He manages <a href="http://www.aprendexojo.com">AprendeXojo.com</a> and is the developer behind the GuancheMOS plug-in for Xojo Developers, Markdown Parser for Xojo, HTMLColorizer for Xojo and the Snippery app, among others</em></p>
<p>*<a href="https://www.aprendexojo.com/2018/06/comprobar-y-anadir-valores-en-tiempo-de-ejecucion/">Read this post in Spanish</a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
