<?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>Delegates &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/delegates/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, 23 Sep 2025 19:39:03 +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>Building Blocks for Beginners: Modules, Classes, Interfaces, and Delegates</title>
		<link>https://blog.xojo.com/2025/09/24/building-blocks-for-beginners-modules-classes-interfaces-and-delegates/</link>
		
		<dc:creator><![CDATA[Gabriel Ludosanu]]></dc:creator>
		<pubDate>Wed, 24 Sep 2025 15:50:00 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Beginner Tips]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Delegates]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[Multi-Platform Development]]></category>
		<category><![CDATA[Software Development]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=15381</guid>

					<description><![CDATA[If terms like “Modules,” “Classes,” “Interfaces,”, “Delegates” feel new or abstract, keep this article as a map. I’ll explain what each one does, why they&#8230;]]></description>
										<content:encoded><![CDATA[
<p>If terms like “Modules,” “Classes,” “Interfaces,”, “Delegates” feel new or abstract, keep this article as a map. I’ll explain what each one does, why they matter, and how to choose the right one. Think of these features as your toolkit:</p>



<ul class="wp-block-list">
<li><strong>Modules</strong>: organize shared helpers that don’t store data. They just do a job and return a result.</li>



<li><strong>Classes</strong>: represent objects that keep data and have actions.</li>



<li><strong>Interfaces</strong>: agreements about what methods exist so parts can work together without caring how they’re built.</li>



<li><strong>Delegates</strong>: a way to pass a function around so another part of your app can call it later (a callback).</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading" id="modules">Modules</h2>



<p><strong>Purpose</strong></p>



<ul class="wp-block-list">
<li>Provide a&nbsp;<strong>namespace</strong>&nbsp;for related helpers, constants, and enums.</li>



<li>Offer reusable helpers that&nbsp;<strong>don’t remember data</strong>&nbsp;between calls.</li>



<li>Add&nbsp;<strong>extension methods</strong>&nbsp;that make calling code cleaner.</li>
</ul>



<p><strong>Use when</strong></p>



<ul class="wp-block-list">
<li>You need shared helpers (formatting, parsing, conversions).</li>



<li>You want to group constants/enums (error codes, log levels).</li>



<li>You’re adding behavior to existing types via extension methods.</li>
</ul>



<p><strong>Avoid when</strong></p>



<ul class="wp-block-list">
<li>You’re tempted to store&nbsp;changeable global data&nbsp;that creates hidden dependencies.</li>
</ul>



<p><strong>Benefits</strong></p>



<p>Read more: <a href="https://documentation.xojo.com/getting_started/using_the_xojo_language/modules.html" target="_blank" rel="noreferrer noopener">Xojo Modules Documentation</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading" id="classes">Classes</h2>



<p><strong>Purpose</strong></p>



<ul class="wp-block-list">
<li>Represent real concepts in your app (e.g., an order, a report, a session).</li>



<li>Keep data and the related rules/actions in one place.</li>



<li>Separate concerns: core logic in classes, screen logic in the UI.</li>
</ul>



<p><strong>Use when</strong></p>



<ul class="wp-block-list">
<li>You have data that changes over time and rules to enforce.</li>



<li>You need&nbsp;<strong>services</strong>&nbsp;that coordinate steps (for example, “Export report” or “Sync data”).</li>



<li>You want testable units that don’t depend on UI elements.</li>
</ul>



<p><strong>Avoid when</strong></p>



<ul class="wp-block-list">
<li>You only need a simple helper that returns a result (that belongs in a Module).</li>



<li>Platform-specific calls would leak into core logic (hide those behind Interfaces).</li>
</ul>



<p><strong>Benefits</strong></p>



<ul class="wp-block-list">
<li>Clear rules kept close to the data they protect.</li>



<li>Clean boundaries between UI, core logic, and data access.</li>
</ul>



<p>Read more: <a href="https://documentation.xojo.com/getting_started/object-oriented_programming/oop_classes.html" target="_blank" rel="noreferrer noopener">Xojo OOP Classes Documentation</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading" id="interfaces">Interfaces</h2>



<p><strong>Purpose</strong></p>



<ul class="wp-block-list">
<li>Define the&nbsp;<strong>what</strong>&nbsp;without the&nbsp;<strong>how</strong>.</li>



<li>Let you&nbsp;<strong>swap implementations</strong>&nbsp;(e.g., memory vs. SQLite vs. REST) without changing calling code.</li>



<li>Make testing simple by substituting&nbsp;<strong>fakes/mocks</strong>.</li>
</ul>



<p><strong>Use when</strong></p>



<ul class="wp-block-list">
<li>A feature may vary by platform or environment (file system, keychain, camera, notifications).</li>



<li>You want to keep app logic separate from the details of databases, files, or network calls.</li>



<li>You’ll have multiple strategies that share the same shape.</li>
</ul>



<p><strong>Avoid when</strong></p>



<ul class="wp-block-list">
<li>There’s only one implementation and you don’t need to test in isolation.</li>
</ul>



<p><strong>Benefits</strong></p>



<ul class="wp-block-list">
<li>Loose coupling&nbsp;and easier future changes.</li>



<li>Cleaner, more stable internal APIs.</li>
</ul>



<p>Read more: <a href="https://documentation.xojo.com/getting_started/object-oriented_programming/interfaces.html" target="_blank" rel="noreferrer noopener">Xojo Interfaces Documentation</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading" id="delegates">Delegates</h2>



<p><strong>Purpose</strong></p>



<ul class="wp-block-list">
<li>Provide&nbsp;<strong>type-safe callbacks</strong>&nbsp;by letting you hand a function to another part of your app.</li>



<li>Enable flexible behavior without defining a whole interface and class.</li>



<li>Power progress updates, cancel handlers, filters, or small strategies.</li>
</ul>



<p><strong>Use when</strong></p>



<ul class="wp-block-list">
<li>You need a&nbsp;<strong>one-off callback</strong>&nbsp;(progress, completion, error reporting).</li>



<li>You want to inject small pieces of behavior (sorting, filtering, formatting).</li>



<li>You’re wiring up dynamic behavior at runtime.</li>
</ul>



<p><strong>Avoid when</strong></p>



<ul class="wp-block-list">
<li>You need a broader contract with multiple related methods (use an Interface instead).</li>



<li>You need lifecycle or semantic grouping (a Class with&nbsp;events&nbsp;may be clearer).</li>
</ul>



<p><strong>Benefits</strong></p>



<ul class="wp-block-list">
<li>Lightweight and expressive.</li>



<li>Less boilerplate than creating full classes for simple callbacks.</li>



<li>Encourages small, composable, testable pieces.</li>
</ul>



<p>Read more: <a href="https://documentation.xojo.com/getting_started/using_the_xojo_language/advanced_language_features.html#delegates" target="_blank" rel="noreferrer noopener">Xojo Delegates Documentation</a></p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h2 class="wp-block-heading" id="quick-decision-guide">Quick Decision Guide</h2>



<ul class="wp-block-list">
<li><strong>I need to group common helpers and constants.</strong>&nbsp;→ Module</li>



<li><strong>I’m modeling an object with data and rules.</strong>&nbsp;→ Class</li>



<li><strong>I want to depend on behavior, not a specific implementation.</strong>&nbsp;→ Interface</li>



<li><strong>I need a small callback or injected behavior.</strong>&nbsp;→ Delegate</li>



<li><strong>I need many subscribers to a lifecycle change.</strong>&nbsp;→ Event on a class</li>
</ul>



<h3 class="wp-block-heading" id="quick-glossary">Quick glossary</h3>



<ul class="wp-block-list">
<li><strong>Interface</strong>: a contract, a named list of methods something promises to have.</li>



<li><strong>Delegate</strong>: a variable that holds a function to call later (a callback).</li>



<li><strong>Event</strong>: a signal that something happened; other parts can listen for it.</li>
</ul>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>Use&nbsp;Modules&nbsp;to organize shared helpers,&nbsp;Classes&nbsp;to model your world,&nbsp;Interfaces&nbsp;to decouple and swap implementations, and&nbsp;Delegates&nbsp;to pass behavior flexibly. Combined thoughtfully, these features keep Xojo projects easy to change, easy to test, and ready to ship across platforms.</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>Using AddHandler and Delegates in Xojo</title>
		<link>https://blog.xojo.com/2024/10/07/using-addhandler-and-delegates-in-xojo/</link>
		
		<dc:creator><![CDATA[Martin T.]]></dc:creator>
		<pubDate>Mon, 07 Oct 2024 15:00:00 +0000</pubDate>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[Guest Post]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[AddHandler]]></category>
		<category><![CDATA[Delegates]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=13844</guid>

					<description><![CDATA[In Xojo, there are different ways to control the behavior of methods, events, and callbacks. Two commonly used mechanisms are AddHandler and Delegates. Both are&#8230;]]></description>
										<content:encoded><![CDATA[
<p>In Xojo, there are different ways to control the behavior of methods, events, and callbacks. Two commonly used mechanisms are <em>AddHandler </em>and <em>Delegates</em>. Both are designed to dynamically reference and invoke functions or methods at runtime. Although they share similar goals, they differ in implementation and usage. In this article, we will explain the similarities and differences between <em>AddHandler </em>and <em>Delegates </em>in Xojo and demonstrate how you can achieve the same result using both approaches.</p>



<h2 class="wp-block-heading">What are Delegates?</h2>



<p>A Delegate in Xojo is essentially a “pointer” to a method. It allows you to dynamically reference a method, treating it as if it were a variable. This means that a method can be passed to another method or function like a data variable.</p>



<pre class="wp-block-code"><code>' Defining a Delegate
Delegate Sub MyDelegateMethod(value As Integer)

' Method to be called
Sub PrintNumber(value As Integer)
  MessageBox("Number: " + value.ToString)
End Sub

' Using the Delegate
Sub UseDelegate()
  Var d As MyDelegateMethod = AddressOf PrintNumber
  d.Invoke(10) ' Calls PrintNumber with the value 10
End Sub</code></pre>



<p>In this example, a delegate <em>MyDelegateMethod </em>is defined to reference a method with an Integer parameter. Then, the method <em>PrintNumber </em>is assigned to the delegate variable <em>d</em>, and it is invoked using <em>d.Invoke()</em>.</p>



<h2 class="wp-block-heading">What is AddHandler?</h2>



<p>The <em>AddHandler </em>command is used in Xojo to dynamically associate a method with an event. Instead of assigning an event handler in the design phase, this can be done at runtime. This allows for more flexible programming, especially in situations where event handling routines need to be assigned or changed dynamically.</p>



<pre class="wp-block-code"><code>' Method that acts as an event handler
Sub ButtonPressed(sender As DesktopButton)
  MessageBox("Button was pressed!")
End Sub

' Using AddHandler
Sub SetupButtonHandler(button As DesktopButton)
  AddHandler button.Pressed, AddressOf ButtonPressed
End Sub</code></pre>



<p>In this example, the <em>ButtonPressed </em>method is dynamically associated with the <em>Pressed </em>event of a DesktopButton. When the button is pressed, the <em>ButtonPressed </em>method shows a message.</p>



<h3 class="wp-block-heading">Important Note: Using RemoveHandler with AddHandler</h3>



<p>Whenever you use <em>AddHandler</em>, it’s important to remember to use <em>RemoveHandler </em>to properly un-assign the event handler when it’s no longer needed. If you fail to remove the handler, you may encounter memory leaks or unexpected behavior, especially when working with objects that are frequently created and destroyed.</p>



<pre class="wp-block-code"><code>' Removing the event handler
Sub RemoveButtonHandler(button As DesktopButton)
  RemoveHandler button.Pressed, AddressOf ButtonPressed
End Sub</code></pre>



<p>In this case, <em>RemoveHandler </em>is used to detach the <em>ButtonPressed </em>method from the <em>Pressed </em>event of the DesktopButton. This is especially useful when a button or event source is about to be destroyed or when you no longer want the handler to respond to events.</p>



<h2 class="wp-block-heading">Similarities between AddHandler and Delegates</h2>



<ol class="wp-block-list">
<li><strong>Dynamic Behavior:</strong> Both <em>Delegates </em>and <em>AddHandler </em>allow for the dynamic assignment of methods, providing more flexibility in the flow of the program.</li>



<li><strong>Method Referencing:</strong> Both concepts use the <em>AddressOf </em>syntax to reference a method.</li>



<li><strong>Flexibility:</strong> Both mechanisms are useful for reacting to specific events or functions at runtime.</li>
</ol>



<h2 class="wp-block-heading">Differences between AddHandler and Delegates </h2>



<p>1. <strong>Purpose:</strong></p>



<ul class="wp-block-list">
<li>Delegates are more general and are used to treat methods as variables and pass them as parameters to other methods.</li>



<li>AddHandler is specifically designed for event handling. It is used to assign methods to events at runtime.</li>
</ul>



<p>2. <strong>Target Structure:</strong></p>



<ul class="wp-block-list">
<li>Delegates are strongly typed. They require an exact match between the method signature and the delegate.</li>



<li>AddHandler binds methods to events, which require a predetermined signature but are dictated by the context of the object used.</li>
</ul>



<p>3. <strong>Lifetime:</strong></p>



<ul class="wp-block-list">
<li>Delegates can exist as standalone variables within any scope.</li>



<li>AddHandler is tied to the event and object it is associated with. If the event or object is destroyed, the handler is no longer called.</li>
</ul>



<h2 class="wp-block-heading">Achieving the same functionality with both approaches</h2>



<p></p>



<h3 class="wp-block-heading">Event Handling with AddHandler</h3>



<p>Let’s assume we have a timer event and want to specify at runtime which method should be called when the timer elapses.</p>



<pre class="wp-block-code"><code>' Method called by the Timer event
Sub TimerAction(sender As Timer)
  MessageBox("Timer elapsed!")
End Sub

' Dynamically assigning the event with AddHandler
Sub SetupTimer(timer As Timer)
  #If TargetMobile
    AddHandler timer.Run, AddressOf TimerAction
  #Else
    AddHandler timer.Action, AddressOf TimerAction
  #EndIf

  timer.RunMode = Timer.RunModes.Multiple
  timer.Period = 1000
End Sub</code></pre>



<p>In this example, the <em>TimerAction </em>method is assigned to the Run/Action event of a timer at runtime.</p>



<h3 class="wp-block-heading">Same Functionality with Delegates</h3>



<p>Now, the same logic but using Delegates:</p>



<pre class="wp-block-code"><code>' Defining a Delegate for Timer actions
Delegate Sub TimerDelegate(sender As Timer)

' Method to be called
Sub TimerAction(sender As Timer)
  MessageBox("Timer elapsed!")
End Sub

' Assigning and invoking the method via a Delegate
Sub SetupTimerWithDelegate(timer As Timer)
  Var d As TimerDelegate = AddressOf TimerAction

  #If TargetMobile
    AddHandler timer.Run, d
  #Else
    AddHandler timer.Action, d
  #EndIf

  timer.RunMode = Timer.RunModes.Multiple
  timer.Period = 1000
End Sub</code></pre>



<p>In this case, a Delegate is used to reference the same <em>TimerAction </em>method, but the structure remains nearly identical.</p>



<p><em>AddHandler </em>and <em>Delegates </em>both provide ways to dynamically handle methods at runtime in Xojo. While <em>Delegates </em>are more flexible and general-purpose, <em>AddHandler </em>is specifically designed for dynamic event handling. Both mechanisms are useful depending on the use case. The main difference lies in how they are used and their target structure: <em>Delegates </em>are strongly typed and can be used anywhere in the code, whereas <em>AddHandler </em>is used to link methods to events dynamically.</p>



<p>Additionally, when using <em>AddHandler</em>, it is crucial to remember to use <em>RemoveHandler </em>to properly unbind event handlers when they are no longer needed. Failing to do so can lead to performance issues, memory leaks, or unintended behavior, especially when dealing with dynamic or temporary objects.</p>



<p>By combining these two techniques, you can make Xojo programs more flexible and dynamic.</p>



<p>Happy Coding!</p>



<p><em>Martin T. is a Xojo MVP and has been very involved in testing Android support.</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>The Powerful World of Delegates</title>
		<link>https://blog.xojo.com/2020/03/11/the-powerful-world-of-delegates/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Wed, 11 Mar 2020 10:00:00 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Delegates]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=6650</guid>

					<description><![CDATA[Delegates are one of those advanced features you can find in the Xojo programming language, and it's a really powerful one! Once you discover and comprehend all the flexibility you can get from Delegates, I'm pretty sure you'll start to start using them! Through the use of Delegates, you can dynamically change the code to execute.]]></description>
										<content:encoded><![CDATA[<p>Delegates are one of those advanced features you can find in the Xojo programming language, and it&#8217;s a really powerful one! Once you discover and comprehend all the flexibility you can get from Delegtes, I&#8217;m pretty sure you&#8217;ll start to start using them!</p>
<p>But what is a <code>Delegate</code>? We can define it as a data type definition for a method (or function) signature. That is: when you define a new delegate type you need to provide it with a concrete method or function signature. And here is where everything begins.</p>
<p>Once you&#8217;ve defined the new Delegate type, you can use it in your code as you would with any other data type. So, for example, you can add a property to any object (Windows, WebPage, iOSView, Class, subclass…) and define its type as the just-created Delegate type.</p>
<p>That means that during your app&#8217;s code execution, you can change, for example, the Delegate property value so it stores ANY method or function matching the Delegate signature and then, you can invoke it!&nbsp;The main point here is that, through the use of Delegates, you can dynamically change the code to execute.</p>
<p>Let&#8217;s see how it works through a simple example, randomly drawing to a Graphic class instance using that code will work with any graphic instance you pass along as parameter.</p>
<h2>Delegate Creation</h2>
<p>Of course, the first step is defining our Delegate data type. In a new Desktop project, select Insert &gt; Delegate, and use the Inspector Panel to change its values to:</p>
<ul>
<li><strong>Delegate Name:</strong> FigureDelegate</li>
<li><strong>Parameters:</strong> g as Graphics</li>
</ul>
<p>Voilà! We have created a new data type that can point to any method matching that signature.</p>
<h2>Using the Delegate Type</h2>
<p>As you do with any of the standard data types or your created classes, you can create new variables or properties whose data type is our delegate type. For example, add a new property to the Window1 item and change its definition in the Panel Inspector so it reads:</p>
<ul>
<li><strong>Name:</strong> Delegates()</li>
<li><strong>Type:</strong> FigureDelegate</li>
</ul>
<p>Our Delegate property will be an Array storing FigureDelegates values.</p>
<h2>Creating Matching Methods</h2>
<p>Now let&#8217;s add a few methods matching our Delegate signature, so we can add them as values to our Delegates() Array.</p>
<p>The first one will randomly draw a line in the received Graphics context, as parameter. So, add a new method to Window1 and use the Inspector panel to change its values to:</p>
<ul>
<li><strong>MethodName:</strong> DrawLine</li>
<li><strong>Parameters:</strong> g as Graphics</li>
</ul>
<p>And write the following code in the associated Code Editor:</p>
<pre>g.ClearRectangle(0,0,g.Width,g.Height)

Var rd As New random
Var x, y, w, h As Double

g.PenSize = 2
g.DrawingColor = RGB(rd.InRange(0,255), rd.InRange(0,255), rd.InRange(0,255))

x = rd.InRange(0, g.Width)
y = rd.InRange(0, g.Height)
w = rd.InRange(0, g.Width)
h = rd.InRange(0, g.Height)

g.drawline(x,y,w,h)</pre>
<p>Our second method will draw Ovals; so, add a new method to the Window1 item and change its values to read:</p>
<ul>
<li><strong>Method Name:</strong> DrawOval</li>
<li><strong>Parameters:</strong> g as Graphics</li>
</ul>
<p>And write the following code in the associated Code Editor:</p>
<pre>g.ClearRectangle(0,0,g.Width,g.Height)

Var rd As New random
Var x, y, w, h As Double

g.PenSize = 2
g.DrawingColor = rgb(rd.InRange(0,255), rd.InRange(0,255), rd.InRange(0,255))

Var widthAvail As Integer
Var heightAvail As Integer

x = rd.InRange(0, g.Width)
y = rd.InRange(0, g.Height)
widthAvail = g.Width - x
heightAvail = g.Height - y
w = rd.InRange(0, widthAvail)
h = rd.InRange(0, heightAvail)
g.DrawOval(x,y,w,h)</pre>
<p>Next, add a third method. This one will draw a string. Once added to the Window1 item, use the Inspector Panel to change its values to:</p>
<ul>
<li><strong>Method Name:</strong> DrawText</li>
<li><strong>Parameters:</strong> g as Graphics</li>
</ul>
<p>And put the following snippet of code in the associated Code Editor:</p>
<pre>g.ClearRectangle(0,0,g.Width,g.Height)

Static t As Text = "Hello World!"
Var tc As Double = g.TextWidth(t)/2

g.DrawText(t, g.Width/2-tc, g.Height/2-g.TextHeight/2)</pre>
<h2></h2>
<h2>Adding Methods to the Delegates() type</h2>
<p>We told that a Delegate data type points to a Method or Function; but, how can we get that value? Really simple! Xojo provides the AddressOf operator that gives us the memory address where the method &#8220;lives&#8221;.</p>
<p>Add the Open Event Handler to the Window1 item and add the following code to the associated Code Editor:</p>
<pre>Image = New Picture(ImageWell1.Width, ImageWell1.Height, 32)
Delegates.AddRow(AddressOf DrawLine)
Delegates.AddRow(AddressOf DrawOval)
Delegates.AddRow(AddressOf DrawText)</pre>
<h2></h2>
<h2>Adding a Picture Instance</h2>
<p>We will need a Graphic context to paint in. For that we will add a new Property to our Window1 item, using the Inspector Panel to change its values to:</p>
<ul>
<li><strong>Name:</strong> Image</li>
<li><strong>Type:</strong> Picture</li>
</ul>
<h2>Layout the User Interface</h2>
<p>Now let&#8217;s take care of the user interface, so we can see how everything works.</p>
<p>Select Window1 to bring in the Layout Editor for the default application window. Then, add an ImageWell and a PushButton control from the Library to the Layout Editor. Change their positions and the ImageWell size so they matche the following screenshot:</p>
<p><img fetchpriority="high" decoding="async" class="wp-image-6652 size-medium aligncenter" src="https://blog.xojo.com/wp-content/uploads/2020/02/Captura-de-pantalla-2020-02-21-a-las-10.58.38-300x225.png" alt="" width="300" height="225" srcset="https://blog.xojo.com/wp-content/uploads/2020/02/Captura-de-pantalla-2020-02-21-a-las-10.58.38-300x225.png 300w, https://blog.xojo.com/wp-content/uploads/2020/02/Captura-de-pantalla-2020-02-21-a-las-10.58.38.png 692w" sizes="(max-width: 300px) 100vw, 300px" /></p>
<p>Finally, add the Action Event Handler to the just added PushButton and write the following snippet of code in the associated Code Editor:</p>
<pre>Var rd As New Random
Var d As FigureDelegate

d = Delegates( rd.InRange(0, Delegates.LastRowIndex) )

d.Invoke( image.Graphics )

ImageWell1.Image = Image</pre>
<p>As you can see, we defined a new FigureDelegate variable and assigned it a random value from our Delegates Array. Then, we only need to call the method (or function) pointed by a Delegate data type using the Invoke method on it.&nbsp;Run the example app and see how it behaves.</p>
<p>As you can see, using this technique you could use the same methods on, for example, any picture instance.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
