<?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>Extension Methods &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/extension-methods/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Tue, 17 Mar 2026 09:06: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>Your Framework, Your Rules: Why I Use Extends for Everything</title>
		<link>https://blog.xojo.com/2026/03/17/your-framework-your-rules-why-i-use-extends-for-everything/</link>
		
		<dc:creator><![CDATA[Gabriel Ludosanu]]></dc:creator>
		<pubDate>Tue, 17 Mar 2026 15:00:00 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Code Organization]]></category>
		<category><![CDATA[Extends]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=15904</guid>

					<description><![CDATA[Over the weekend, I was deep into a side project, parsing a massive server log dump. Strings were everywhere: timestamps were mangled, JSON blobs were&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Over the weekend, I was deep into a side project, parsing a massive server log dump. Strings were everywhere: timestamps were mangled, JSON blobs were half-broken&#8230;I kept writing these clunky utility calls, nesting functions inside functions, my code was turning into a sideways pyramid of parentheses.</p>



<p>Then I remembered&nbsp;<code>Extends</code>.</p>



<p>It&#8217;s one of those features that sits quietly in the language. But once it clicks, it changes how you think about utility code entirely. Using Extends, within twenty minutes, my nested mess became readable.</p>



<h2 class="wp-block-heading" id="the-moment-it-clicks">Extends Methods in Practice</h2>



<p>You know the feeling. You&#8217;re writing&nbsp;<code>ParseLogEntry(rawLine)</code>&nbsp;for the hundredth time, and something feels off. The function works. It&#8217;s in your Utilities module (maybe part of a Library?). But every time you call it, there&#8217;s this tiny friction. Why doesn&#8217;t String just&nbsp;<em>know</em>&nbsp;how to parse itself into a log entry? That&#8217;s what&nbsp;<code>Extends</code>&nbsp;fixes.</p>



<p>It lets you teach existing classes new tricks. You write a method in a module, tag the first parameter with&nbsp;<code>Extends</code>, and suddenly&nbsp;<code>String</code>,&nbsp;<code>DateTime</code>,&nbsp;<code>Dictionary</code>&nbsp;(whatever you need) behaves like it always had that method. No subclassing. No wrapper classes. Just the framework, extended to fit&nbsp;<em>your</em>&nbsp;brain.</p>



<h2 class="wp-block-heading" id="why-it-matters">Reading The Code</h2>



<p>Code is read far more than it&#8217;s written. We both know this. But we still write code that reads backwards.</p>



<pre class="wp-block-code"><code>// The old way
Var result As DateTime = ExtractTimestamp(CleanLogEntry(rawLine))

// With Extends
Var result As DateTime = rawLine.CleanLogEntry.ExtractTimestamp</code></pre>



<p>When you&#8217;re debugging at 2 AM, tracing logic through nested function calls is the last thing you want. Extension methods turn that horizontal scan into a vertical one. </p>



<h2 class="wp-block-heading" id="when-i-reach-for-it">When Extends is Exactly Right</h2>



<p>I don&#8217;t use&nbsp;<code>Extends</code>&nbsp;for everything; it&#8217;s not a replacement for proper inheritance or composition. But there are specific moments when it&#8217;s exactly right.</p>



<p><strong>Built-in types that need one more method.</strong>&nbsp;For my log parser, I needed to extract timestamps, clean junk characters, and pull out ISO dates. So, I extended the String class with my custom utilities and made it feel native.</p>



<p><strong>Framework classes you can&#8217;t touch.</strong>&nbsp;You can&#8217;t subclass everything, but <code>Extends</code> can work around things like things third-party plugins, legacy code.</p>



<p><strong>APIs you wish existed.</strong>&nbsp;I&#8217;ve lost count of how many times I&#8217;ve wanted a property or method on a framework class that just isn&#8217;t there.&nbsp;<code>Extends</code>&nbsp;lets me do just that.</p>



<p>For practical examples like email validation, URL validation, and dictionary helpers, check out this&nbsp;<a href="https://blog.xojo.com/2020/06/03/using-class-extensions-to-validate-email-and-url-data/">article</a>&nbsp;that walks you through real implementations. If you want code you can copy into your project today, it&#8217;s a good place to start.</p>



<h2 class="wp-block-heading" id="a-few-gotchas">A Few Things To Keep In Mind</h2>



<p><strong>Nil doesn&#8217;t forgive.</strong> Call an extension on a Nil object and you&#8217;ll get a <code>NilObjectException</code> before your method even runs. The extension sits on the right side of the dot. That object needs to exist.</p>



<p><strong>No override.</strong>&nbsp;You can&#8217;t replace built-in methods. If you try creating an extension with the same name as an existing method, the compiler will use the original. That&#8217;s a good thing; it means you can&#8217;t accidentally break framework behavior.</p>



<p><strong>No properties.</strong>&nbsp;Extensions add behavior, not state. If you need to store data, you still need a subclass or a wrapper.</p>



<p><strong>Stay organized.</strong>&nbsp;Put extensions in well-named modules like&nbsp;<code>LogParsingExtensions</code>&nbsp;or&nbsp;<code>StringHelpers</code>. Six months from now, you&#8217;ll want to know where that&nbsp;<code>.ExtractTimestamp</code>&nbsp;method came from.</p>



<p><strong>Dependencies can hide.</strong>&nbsp;Extension methods live in modules and behave like native methods, so it&#8217;s not always obvious which module a class depends on. Good naming helps.</p>



<h2 class="wp-block-heading" id="try-it">Trying Extends</h2>



<p>If you&#8217;ve never written an extension method, take five minutes. Add a module to your project and create a method like this:</p>



<pre class="wp-block-code"><code>Public Function ExtractFirstISODate(<strong>Extends source As String</strong>) As DateTime
  Var rx As New RegEx
  rx.SearchPattern = "(\d{4}-\d{2}-\d{2})&#91;Tt]?(\d{2}:\d{2}:\d{2})"
  
  Var match As RegExMatch = rx.Search(source)
  If match &lt;&gt; Nil And match.SubExpressionCount &gt; 1 Then
    Var datePart As String = match.SubExpressionString(1)
    Var timePart As String = match.SubExpressionString(2)
    Var isoString As String = datePart + " " + timePart
    
    Try
      Return DateTime.FromString(isoString)
    Catch err As RuntimeException
      Return Nil
    End Try
  End If
  Return Nil
End Function</code></pre>



<p>Now you can call it directly on any string:</p>



<pre class="wp-block-code"><code>Var logLine As String = "&#91;2025-08-14T14:32:15] ERROR: Connection timeout"

Var timestamp As DateTime = logLine.<strong>ExtractFirstISODate</strong>

If timestamp &lt;&gt; Nil Then
  MessageBox("Found timestamp: " + timestamp.ToString)
Else
  MessageBox("No valid timestamp found")
End If</code></pre>



<p>That&#8217;s it. No utility class to instantiate. No module name to remember. Just&nbsp;<code>stringVar.ExtractFirstISODate</code>.</p>



<p>For more complete examples, check out the&nbsp;<a href="https://documentation.xojo.com/getting_started/using_the_xojo_language/modules.html#getting-started-using-the-xojo-language-modules-extension-methods" target="_blank" rel="noreferrer noopener">Extension Methods topic</a>&nbsp;in the Xojo Documentation.</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>WebUIControl Badges Using Extension Methods</title>
		<link>https://blog.xojo.com/2022/11/03/webuicontrol-badges-using-extension-methods/</link>
		
		<dc:creator><![CDATA[Ricardo Cruz]]></dc:creator>
		<pubDate>Thu, 03 Nov 2022 17:59:19 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Beginner Tips]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Rapid Application Development]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[webdev]]></category>
		<category><![CDATA[Xojo API 2.0]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=10934</guid>

					<description><![CDATA[I was having some fun with badges, trying to recreate a design. I’ve ended up writing a few extension methods to use them on almost any WebUIControl. ]]></description>
										<content:encoded><![CDATA[
<p>I was having some fun with badges, trying to recreate a design. I’ve ended up writing a few extension methods to use them on almost any WebUIControl. </p>



<figure class="wp-block-image size-large is-style-default"><img fetchpriority="high" decoding="async" width="1024" height="429" src="https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-1024x429.png" alt="" class="wp-image-10936" srcset="https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-1024x429.png 1024w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-300x126.png 300w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-768x321.png 768w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-1536x643.png 1536w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.12-AM-2048x857.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<h3 class="wp-block-heading">But first, what’s an Extension Method anyway? </h3>



<p>There are situations where you want to add functionality to a Xojo Framework class. Let’s say we want to add a method to every visual web control. We know that every visual control inherits from WebUIControl. This class can’t be modified, as it’s part of the framework. One option would be subclassing each control and adding new methods… but that’s so boring… </p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="1024" height="577" src="https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.21-AM-1024x577.png" alt="" class="wp-image-10937" srcset="https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.21-AM-1024x577.png 1024w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.21-AM-300x169.png 300w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.21-AM-768x433.png 768w, https://blog.xojo.com/wp-content/uploads/2022/11/Screen-Shot-2022-11-03-at-9.24.21-AM.png 1342w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><a href="https://documentation.xojo.com/getting_started/using_the_xojo_language/modules.html#extension-methods">Extension Methods</a> are part of the Modules feature, allowing us to extend classes with methods, even if we don’t have access to their source code.</p>



<h3 class="wp-block-heading">Adding SetBadge and RemoveBadge methods to every visual control</h3>



<p>The first thing you need is a Module. Press on the Add button from the toolbar and select Module. The name of the module is up to you, I’ll be using WebExtensions in this example.</p>



<p>Now add a new method into that module, called &#8220;SetBadge&#8221;. Then, in the inspector, ensure its visibility is set to Global, instead of Public or Protected.</p>



<p>The magic happens in the first parameter of this method. “Extends” is the reserved keyword for this feature, for example:</p>



<pre class="wp-block-preformatted">Extends myInstance As ExampleClass</pre>



<p>Above method would be extending an imaginary class called “ExampleClass”, and its instance would be available inside that method, accessible from the variable “myInstance”.</p>



<p>In our case, for SetBadge, we want to use this method signature instead:</p>



<pre class="wp-block-preformatted">Extends control As WebUIControl, caption As String, indicator As String = "danger", top As Integer = -10, right As Integer = -10</pre>



<p>Inside, paste the method contents:</p>



<pre class="wp-block-preformatted">Var classes() As String = Array("xojo-badge", "d-flex", "p-2", _ 
"border", "border-light", "bg-" + indicator) 
classes.Add(If(caption.Length &lt; 2, "rounded-circle", "rounded-pill"))

Var styles() As String = Array("height: 21px", "font-size: 10px", _ 
"align-items: center", "color: white", "position: absolute", _ 
"top: " + top.ToString + "px", "right: " + Right.ToString + "px", _ 
"z-index: 10")

If caption = "" Or caption = " " Then caption = "&amp;nbsp;"
Var html As String = "&lt;span class=""" + String.FromArray(classes) + _
""" style=""" + String.FromArray(styles, ";") + """&gt;" + caption + "&lt;/span&gt;"

Var js() As String
js.Add("$('#" + control.ControlID + " .xojo-badge').remove();") 
js.Add("$('#" + control.ControlID + "').append(`" + html + "`);")
control.ExecuteJavaScript(String.FromArray(js))</pre>



<p>What this little sorcery does is appending a new HTML layer into our control, using some Bootstrap’s component CSS classes. Did you notice we are using &#8220;control&#8221; instead of &#8220;Self&#8221;? It matches the “control” variable from the method definition.</p>



<p>Add another Extension Method for removing the badges, called “RemoveBadge”.</p>



<p>The method definition reads like this:</p>



<pre class="wp-block-preformatted">Extends control As WebUIControl</pre>



<p>The content is a bit easier this time:</p>



<pre class="wp-block-preformatted">Var js As String = "$('#" + control.ControlID + " .xojo-badge').remove();"
control.ExecuteJavaScript(js)</pre>



<p>That’s it. Plumbing done.</p>



<p>Now, if you have a WebButton called&#8230; &#8220;AwesomeButton&#8221;, you can set its badge using this code:</p>



<pre class="wp-block-preformatted">AwesomeButton.SetBadge("Hello!")</pre>



<p>And to remove it:</p>



<pre class="wp-block-preformatted">AwesomeButton.RemoveBadge()</pre>



<p>You can use these badges in different scenarios. From displaying a pending notifications counter in a WebButton, to validation errors in a WebTextField.</p>



<p>Adjust and play with the code to meet your needs!&nbsp;<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<p>You can download the example from the <a href="https://github.com/piradoiv/xojo-web-badges">GitHub code repository</a>.</p>



<p><em>Ricardo has always been curious about how things work. Growing up surrounded by computers</em> he became interested in <em>web technologies in the dial-up connections era. Xojo has been his secret weapon and language of preference since 2018. When he’s not online, chances are he will be scuba diving … or crocheting amigurumis. Find Ricardo on Twitter <a href="https://web.archive.org/web/20220805000833/https://www.twitter.com/piradoiv" target="_blank" rel="noreferrer noopener">@piradoiv</a>.</em></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>A Functional ForEach</title>
		<link>https://blog.xojo.com/2021/03/22/a-functional-foreach/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Mon, 22 Mar 2021 10:01:00 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Declares]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Loops]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=8185</guid>

					<description><![CDATA[When looping through an array, I am a big fan of using For Each as I find it more readable than using a For loop with a counter and looking up the item in the array with the counter. Sometimes it’s fun to do stuff just because you can! Plus, it makes for a good excuse to learn about some more advanced Xojo features]]></description>
										<content:encoded><![CDATA[
<p>When looping through an array, I am a big fan of using <a href="https://documentation.xojo.com/api/code_execution/for_each...next.html">For Each</a> as I find it more readable than using a For loop with a counter and looking up the item in the array with the counter.</p>



<p>Such a loop might look like this:</p>



<pre class="wp-block-preformatted">Var xdcs() As String = Array("Orlando", "Austin", "Houston", _
"Las Vegas", "Denver", "Miami")

For Each city As String in xdcs
  Display(city)
Next</pre>



<p>The Xojo Developer Conference, aka XDC, is the biggest Xojo event of the year. We&#8217;re looking forward to <a href="https://www.xojo.com/xdc/">XDC London</a> in October 2021. </p>



<p>Here is the code for the Display method:</p>



<pre class="wp-block-preformatted">Public Sub Display(it As String)
  ListBox1.AddRow(it)
End Sub</pre>



<p>But what if you wanted to simplify things even further? Say you want to write code like this:</p>



<pre class="wp-block-preformatted">xdcs.ForEach(Display)</pre>



<p>Is that even possible in Xojo? Yes, it mostly is, with a couple things called <a href="https://documentation.xojo.com/api/language/extends.html">Extension Methods</a> and <a href="https://documentation.xojo.com/api/language/delegate.html">Delegates</a>.</p>



<p>To set this up, add a Module to your project and add a Delegate to the module. A Delegate is a type that is a method declaration. Here are its specifics:</p>



<ul class="wp-block-list"><li>Delegate Name: ForEachString</li><li>Parameters: it As String</li><li>Scope: Global</li></ul>



<p>Any actual method that matches this delegate signature is said to satisfy the delegate. If you look at the Display method above, you’ll see that it matches this delegate’s signature. So the Display method could be passed into a parameter of this delegate type.</p>



<p>In the same module, add a method. This will be an extension method on a string array. An extension method is a method that is called using dot notation on a specific data type (or class), even though it is not specifically part of the data type. This method also takes the delegate defined above as a parameter. Here are the method specifics:</p>



<ul class="wp-block-list"><li>Method Name: ForEach</li><li>Parameters: Extends stringArray() As String, action As ForEachString</li><li>Scope: Global</li></ul>



<p>Its code still uses For Each and calls the delegate like this:</p>



<pre class="wp-block-preformatted">For Each s As String In stringArray
  action.Invoke(s)
Next</pre>



<p>Note: The above method and delegate are designed to work with String arrays. To work with other array types you’d need to add equivalent delegates and ForEach method overloads.</p>



<p>With these pieces now in place, this code (which is almost exactly what we wanted above &#8212; <a href="https://documentation.xojo.com/api/language/addressof.html">AddressOf</a> is used to pass a reference of the Display method to the delegate) now works:</p>



<pre class="wp-block-preformatted">Var xdcs() As String = Array("Orlando", "Austin", "Houston", _
"Las Vegas", "Denver", "Miami")

xdcs.ForEach(AddressOf Display)</pre>



<p>The above code is slightly shorter since it doesn’t need to declare the variable, but was this really worth all this this effort just for that? Well, perhaps not, but <em>I</em> think it’s cool and sometimes it’s fun to do stuff just because you can! Plus, it makes for a good excuse to learn about some more advanced Xojo features. Either way, I hope you found this interesting.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
