<?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>ByRef &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/byref/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, 12 Dec 2024 13:50:49 +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>Android Language Features</title>
		<link>https://blog.xojo.com/2024/12/10/android-language-features/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Tue, 10 Dec 2024 16:37:38 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[2024r4]]></category>
		<category><![CDATA[Android Structures]]></category>
		<category><![CDATA[ByRef]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=13945</guid>

					<description><![CDATA[Android has been updated to support several standard Xojo language features, including: Structures Structures are a fixed-size data structure that can contain properties of simple&#8230;]]></description>
										<content:encoded><![CDATA[
<p>Android has been updated to support several standard Xojo language features, including:</p>



<ul class="wp-block-list">
<li>Structures</li>



<li>ByRef parameters</li>



<li>Operator_Lookup</li>



<li>Operator_Convert</li>
</ul>



<h2 class="wp-block-heading">Structures</h2>



<p>Structures are a fixed-size data structure that can contain properties of simple types, such as Integer, String or Boolean.</p>



<p>Normally you would only use Structures with Declare API calls, but they can also work as a lightweight, project item specific data structure.</p>



<p>For Android, Structures are not fixed-sized data structures and are not appropriate for Declares, but they offer slightly more flexibility, especially with regard to Strings. When you add a String to an Android Structure, the specified size is ignored; you can store any size string in the property.</p>



<p>Usage is the same. Create a Structure in the Structure editor for the project item, adding the properties you want.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="221" src="https://blog.xojo.com/wp-content/uploads/2024/10/image-7-1024x221.png" alt="" class="wp-image-13946" srcset="https://blog.xojo.com/wp-content/uploads/2024/10/image-7-1024x221.png 1024w, https://blog.xojo.com/wp-content/uploads/2024/10/image-7-300x65.png 300w, https://blog.xojo.com/wp-content/uploads/2024/10/image-7-768x166.png 768w, https://blog.xojo.com/wp-content/uploads/2024/10/image-7-1536x332.png 1536w, https://blog.xojo.com/wp-content/uploads/2024/10/image-7-2048x442.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Note that the Offset and Size columns are not relevant in Android since the Structure is not fixed-size.</p>



<p> In code, you create a structure by name:</p>



<pre class="wp-block-code"><code>Var st As StructTest
st.Age = 42
st.Dead = True
st.Name = "Bob Roberts"
st.Weight = 200.6</code></pre>



<p>Since Android Structures are not size-based, the ByteValue, Size and StringValue members are not useful and will raise an UnsupportedOperationException when used.</p>



<h2 class="wp-block-heading">ByRef Parameters</h2>



<p>ByRef parameters allow you to pass a parameter &#8220;by reference&#8221; so that the caller can access its new value if it was changed. By default, parameters are passed ByVal (or &#8220;by value&#8221;) so that the caller does not see any changes made to them in the method.</p>



<p>Use the ByRef keyword in front of the parameter to make it ByRef.</p>



<p>The <a href="https://documentation.xojo.com/api/language/byref.html" data-type="link" data-id="https://documentation.xojo.com/api/language/byref.html">ByRef page in the docs</a> has more information.</p>



<h2 class="wp-block-heading">Operator_Lookup</h2>



<p>Operator_Lookup might be unique to Xojo. When you add an Operator_Lookup method to a class, it essentially tells the compiler to not do a syntax check on method or property names. Instead, if the name that follows the &#8220;.&#8221; is not an actual member of the class, then the name is passed as a string parameter to the Operator_Convert method.</p>



<p>You can do some strange things with Operator_Lookup, including making your code much more difficult to debug. Use this advanced feature wisely.</p>



<p>Learn more about <a href="https://documentation.xojo.com/api/math/operator_lookup.html">Operator_Lookup in the docs</a>.</p>



<h2 class="wp-block-heading">Operator_Convert</h2>



<p><a href="https://documentation.xojo.com/api/math/operator_convert.html">Operator_Convert</a> is used with classes to allow them to either construct a new instance from a value of a different type or to allow them to return a version of itself that is compatible with the destination type.</p>



<p>Here is an example of a Person class that uses Operator_Lookup. The Person class has a property: FullName As String. This is how you might normally create an instance and assign a value to FullName:</p>



<pre class="wp-block-code"><code>// Person is a class with a property FullName As String
Var p1 As New Person
p1.FullName = "Bob Roberts"</code></pre>



<p>Now add an Operator_Convert method to the class like this:</p>



<pre class="wp-block-code"><code>Sub Operator_Convert(name As String)
  Self.FullName = name
End Sub</code></pre>



<p>With that method, you can now assign a string value to a Person variable and it will create an instance and assign the string value to the FullName property. The syntax looks like this:</p>



<pre class="wp-block-code"><code>Var p As Person
p = "Bob Roberts"</code></pre>



<p>You can also add an Operator_Convert method that returns a representation of a class instance as a different type. For example, say you wanted a Person instance to be able to convert itself to a string. First you have to decide what the string would be and in this case it makes sense to use FullName.</p>



<p>Here is that Operator_Convert for Person:</p>



<pre class="wp-block-code"><code>Function Operator_Convert As String
  Return Self.FullName
End Function</code></pre>



<p>This code then assigns a Person instance to a Label&#8217;s text property where it gets assigned what is in the FullName property:</p>



<pre class="wp-block-code"><code>Var p2 As New Person
p2.FullName = "Janet Jane"
Label1.Text = p2</code></pre>



<p>Like with Operator_Lookup, Operator_Convert is an advanced feature. Use it carefully. It certainly allows for some code to be simplified, but it also means that code might not be as clear as it would if it used more standard syntax.</p>



<p><em>Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at <a href="https://goto10.substack.com" target="_blank" rel="noreferrer noopener">Goto 10</a> and </em>on Mastodon @lefebvre@hachyderm.io.</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>Some Follow-Up Regarding ByRef</title>
		<link>https://blog.xojo.com/2019/01/24/some-follow-up-regarding-byref/</link>
		
		<dc:creator><![CDATA[Norman Palardy]]></dc:creator>
		<pubDate>Thu, 24 Jan 2019 20:57:43 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[ByRef]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=5376</guid>

					<description><![CDATA[Some Follow-Up Regarding ByRef. What happens if instead of trying to assign a new array you just alter the values in the array passed in?]]></description>
										<content:encoded><![CDATA[<p>A reader asked me to clarify something about my <a href="https://blog.xojo.com/2019/01/23/byref-vs-reference-types/" target="_blank" rel="noopener">previous post</a>. Their question was:</p>
<blockquote><p>When MyMethod is written as:</p>
<pre>Sub MyMethod( i() as integer )
  i = array(10,20,30)
  system.debuglog CurrentMethodName + " i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))
End Sub</pre>
<p>What happens if instead of trying to assign a new array you just alter the values in the array passed in?</p></blockquote>
<p><span id="more-5376"></span></p>
<p>They are asking what happens if MyMethod is written as follows:</p>
<pre>Sub MyMethod( i() as integer )
  i(0) = 10
  i(1) = 20
  i(2) = 30
  system.debuglog CurrentMethodName + " i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))
End Sub</pre>
<p>Are the contents of the array altered when you get back to the calling method?<br />
To test this, in a new desktop project we have:</p>
<pre>Dim i() As Integer
i = Array(1,2,3)
System.debuglog CurrentMethodName + " i(0) = " + Str(i(0)) + " i(1) = " + Str(i(1)) + " i(2) = " + Str(i(2))
MyMethod(i)
System.debuglog CurrentMethodName + " i(0) = " + Str(i(0)) + " i(1) = " + Str(i(1)) + " i(2) = " + Str(i(2))</pre>
<p>and run this code we see something like:</p>
<pre>9:46:13 AM : My Application Launched
             Window1.Open i(0) = 1 i(1) = 2 i(2) = 3
             Window1.MyMethod i(0) = 10 i(1) = 20 i(2) = 30
             Window1.Open i(0) = 10 i(1) = 20 i(2) = 30
</pre>
<p>And indeed the values changed do remain when we get back to the caller. Why is this the case? We didn&#8217;t use ByRef and still could alter the array?</p>
<p>Recall that ByRef is needed when you want to change the <strong>instance</strong> referred to. And in this case that isn&#8217;t what we&#8217;re doing. We&#8217;re altering the contents, but not the instance itself. We&#8217;re not replacing the entire array with a new one.</p>
<p>This works with classes as well. If you pass an instance in you can alter that instance&#8217; properties based on normal scope rules.</p>
<p>The moral of the story &#8211; ByRef is used when you want to alter the actual item. When its a value type like an integer this alters the value it holds. When its a reference type this would allow you to assign a new instance.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>ByRef vs. Reference Types</title>
		<link>https://blog.xojo.com/2019/01/23/byref-vs-reference-types/</link>
		
		<dc:creator><![CDATA[Norman Palardy]]></dc:creator>
		<pubDate>Wed, 23 Jan 2019 10:00:21 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[ByRef]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=5336</guid>

					<description><![CDATA[Let's talk about the difference between a "reference type" and the BYREF modifier on a passed parameter.]]></description>
										<content:encoded><![CDATA[<p>Let&#8217;s talk about the difference between a reference type and the <a href="https://documentation.xojo.com/api/language/byref.html">ByRef</a> modifier on a passed parameter. I&#8217;ll start with something simple, an integer.</p>
<p>If you have code like:</p>
<pre>dim i as integer = 1
system.debuglog "Before call i = " + str(i)
call MyMethod(i)
system.debuglog "After call i = " + str(i)
</pre>
<p>and MyMethod is written as</p>
<pre>Sub MyMethod( i as integer ) 
  i = i + 1

  system.debuglog CurrentMethodName + " i = " + str(i)
End Sub</pre>
<p>When you run this code you would see<br />
<code><br />
Before call i = 1<br />
MyMethod i = 2<br />
After call i = 1<br />
</code><br />
Following along, before the call i is set to 1. Then the call is made and because i as not passed ByRef, it is passed by value (<a href="https://documentation.xojo.com/api/language/byval.html">ByVal</a>), a copy of the value is sent to MyMethod in a temporary variable also named i &#8211; but this a <em>copy</em> not an original.</p>
<p>The method is run, i is altered to be 2 and the message &#8220;MyMethod i = 2&#8221; printed to the log. Then the method ends, the temporary variable is disposed of and the main code still has the original copy of i so the last message printed showing i is unchanged. To preserve the method&#8217;s change back in the caller you need to make the parameter be ByRef and then the <em>actual</em> variable is passed instead of a new temporary being created.</p>
<p>Now let&#8217;s apply this same sequence to a reference type like an array:</p>
<pre>dim i() as integer = Array(1, 2, 3)
system.debuglog "Before call i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))
call MyMethod(i)
system.debuglog "After call i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))</pre>
<p>and MyMethod is written as</p>
<pre>Sub MyMethod( i() as integer ) 
  i = array(10,20,30)
  system.debuglog CurrentMethodName + " i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))
End Sub</pre>
<p>With a reference type (see my earlier post <a href="https://blog.xojo.com/2018/12/12/what-kind-of-variable-are-you/">What Kind of Variable Are You</a> if you need any help with that) what we are altering is the contents that are referred to &#8211; but not the specific instance that is referred to. So running the code you see this output:</p>
<pre>Before call i(0) = 1 i(1) = 2 i(2) = 3
Window1.MyMethod i(0) = 10 i(1) = 20 i(2) = 30
After call i(0) = 1 i(1) = 2 i(2) = 3</pre>
<p>So just like with the previous integer example, the reference is not permanently altered despite the effort to do so. Again, in order to change what the parameter refers to you <em>must</em> use ByRef.</p>
<p>If MyMethod were written as such you would be altering the reference that the caller refers to.</p>
<pre>Sub MyMethod(byref i() as integer )
  i = array(10, 20, 30)
  system.debuglog CurrentMethodName + " i(0) = " + str(i(0)) + " i(1) = " + str(i(1)) + " i(2) = " + str(i(2))
End Sub</pre>
<p>Which is not the same as a reference type. The output of the above method is this:</p>
<pre>Before call i(0) = 1 i(1) = 2 i(2) = 3
Window1.MyMethod i(0) = 10 i(1) = 20 i(2) = 30
After call i(0) = 10 i(1) = 20 i(2) = 30</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
