<?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 &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/method/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Thu, 13 Sep 2018 21:01:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Using Extension Methods</title>
		<link>https://blog.xojo.com/2013/09/13/using-extension-methods/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Fri, 13 Sep 2013 00:00:00 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Method]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2013/09/13/using-extension-methods/</guid>

					<description><![CDATA[Want a quick and easy way to add capabilities to bulit-in classes and types without subclassing? Try extension methods. An extension method is a method that is called using syntax that indicates it belongs to another object. For example, say you really don't like writing code like this that increments an integer value:]]></description>
										<content:encoded><![CDATA[<p>Want a quick and easy way to add capabilities to bulit-in classes and types without subclassing? Try extension methods.</p>
<p><span id="more-59"></span></p>
<p>An extension method is a method that is called using syntax that indicates it belongs to another object. For example, say you really don&#8217;t like writing code like this that increments an integer value:</p>
<pre>downloadCounter = downloadCounter + 1</pre>
<p>With an extension method for Integer, you could instead write something like this:</p>
<pre>downloadCounter.Add</pre>
<p>Essentially, extension methods are global methods that use the <a href="http://developer.xojo.com/extends">Extends</a> keyword to allow you to write the method using dot notation. This describes the first rule, which is that extension methods must be <strong>global methods</strong> on modules. The extension method for this Add method looks like this:</p>
<pre>Sub Add(ByRef Extends i As Integer, amount As Integer = 1)
  i = i + amount
End Sub</pre>
<p>Since this takes an optional parameter, you could also write:</p>
<pre>downloadCounter.Add(5)</pre>
<p>to mean</p>
<pre>downloadCounter = downloadCounter + 5</pre>
<p>There is a lot more you can do with extension methods, but they are particularly useful for adding methods like this to the built-in data types (such as Integer) that are not available for subclassing.</p>
<p>Another good example is adding a DoubleQuote method that can be used to quote strings:</p>
<pre>Function DoubleQuote(Extends s As String) As String
  Return """" + s + """"
End Function</pre>
<p>Of course, you can also extend regular classes as well. This example extends ListBox to add a search function that will search and select a row that contains a specific column value:</p>
<pre>Sub Find(Extends lb As ListBox, findText As String, column As Integer)
  For i As Integer = 0 To lb.ListCount-1
    If lb.Cell(i, column) = findText Then
      lb.ListIndex = i
      Exit For
    End If
  Next
End Sub</pre>
<p>You use it by supplying the text to find and the column to search in:</p>
<pre>ListBox1.Find("MyValue", 1)</pre>
<p>Extensions are a great way to make your code more readable and to easily add useful &#8220;utility&#8221; methods. To learn more about extension methods, refer to the <a href="http://developer.xojo.com/userguide/modules$Extension%20Methods">User Guide</a>.</p>
<p>There&#8217;s an extension method example that is included with Xojo:</p>
<p>Examples/Language Features/ExtensionMethods</p>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-1f340a96-5baa-4c25-b871-2502beda3b00" class="hs-cta-wrapper"> <span id="hs-cta-1f340a96-5baa-4c25-b871-2502beda3b00" class="hs-cta-node hs-cta-1f340a96-5baa-4c25-b871-2502beda3b00"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://developer.xojo.com/webinar-cool-language-features"><img fetchpriority="high" decoding="async" id="hs-cta-img-1f340a96-5baa-4c25-b871-2502beda3b00" class="hs-cta-img aligncenter" style="border-width: 0px;" src="https://blog.xojo.com/wp-content/uploads/2013/09/1f340a96-5baa-4c25-b871-2502beda3b00.png" alt="Watch Language Features Video" width="641" height="107" /></a> </span><script src="https://js.hscta.net/cta/current.js" charset="utf-8">// <![CDATA[
<script type="text/javascript"><![CDATA[ hbspt.cta.load(608515, '1f340a96-5baa-4c25-b871-2502beda3b00', {}); // ]]&gt;</script></span><br />
<!-- end HubSpot Call-to-Action Code --></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
