<?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>Lazy Loading &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/lazy-loading/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, 02 Mar 2021 17:26:29 +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>A Lazy Computed Property</title>
		<link>https://blog.xojo.com/2020/01/27/a-lazy-computed-property/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Mon, 27 Jan 2020 10:00:00 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Computed Property]]></category>
		<category><![CDATA[Lazy Loading]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=6468</guid>

					<description><![CDATA[Have a class that has an object property that needs to be initialized or instantiated? You can always instantiate the property in the Constructor (or Open event) like this:]]></description>
										<content:encoded><![CDATA[
<p>Do you have a class that has an object property that needs to be initialized or instantiated? You can always instantiate the property in the Constructor (or Open event) like this:</p>



<pre class="wp-block-preformatted">MyClass1 = New MyClass</pre>



<p>But you can also do this using a Computed Property on a Class (I&#8217;m using TestClass for this example).</p>



<p>First, create a private property such as <strong>mMyClass1 As MyClass</strong>. It will be Nil by default as all object properties are.</p>



<p>Then create a Computed Property called <strong>MyClass1 As MyClass</strong>.</p>



<p>In the Get, check if mMyClass1 is Nil. If it is, create it. Then return it.</p>



<pre class="wp-block-preformatted">If mMyClass1 Is Nil Then<br>   mMyClass1 = New MyClass<br>End If<br>Return mMyClass1</pre>



<p>Most likely you’d want this property to be read-only so you would not implement Set (even though the property is read-only, since it is an object you&#8217;ll still be able to modify the properties it contains). But if you did want a way for the object reference to be changed, you can do this in the Set:</p>



<pre class="wp-block-preformatted">mMyClass1 = value</pre>



<p>Now you can make use of MyClass1 on TestClass as shown below. The above technique has two advantages over instantiating the property in the Constructor:</p>



<ol class="wp-block-list"><li>It keeps the code more closely associated with the property.</li><li>The property is only initialized when it is first accessed.</li></ol>



<pre class="wp-block-preformatted">Var tc As New TestClass
tc.MyClass1.TestNumProperty = 42</pre>



<p>This technique is also referred to as <a href="https://en.wikipedia.org/wiki/Lazy_loading">Lazy Loading</a> or Lazy Initialization.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
