<?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>Variants &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/variants/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Mon, 21 Apr 2025 19:28:13 +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>Writing Drawing Code for Android, Desktop, iOS and Web</title>
		<link>https://blog.xojo.com/2025/04/21/writing-drawing-code-for-android-desktop-ios-and-web/</link>
		
		<dc:creator><![CDATA[Geoff Perlman]]></dc:creator>
		<pubDate>Mon, 21 Apr 2025 19:28:10 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Desktop]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Multi-Platform Development]]></category>
		<category><![CDATA[Variants]]></category>
		<category><![CDATA[WebGraphics]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=14826</guid>

					<description><![CDATA[When writing code it&#8217;s always a good idea to be thinking about the future. Today you are creating a desktop project but tomorrow you might&#8230;]]></description>
										<content:encoded><![CDATA[
<p>When writing code it&#8217;s always a good idea to be thinking about the future. Today you are creating a desktop project but tomorrow you might need to create an Android or iOS project. That code you&#8217;re writing might be code you could use for another project for another platform. </p>



<p>For the most part, writing drawing code will <em>just work</em> because you use Xojo&#8217;s <a href="https://documentation.xojo.com/api/graphics/graphics.html#graphics" target="_blank" rel="noreferrer noopener">Graphics</a> class for Android, Desktop and iOS projects. Web projects, however, don&#8217;t use the Graphics class. The <a href="https://documentation.xojo.com/api/user_interface/web/webcanvas.html#description" target="_blank" rel="noreferrer noopener">WebCanvas</a> control&#8217;s <a href="https://documentation.xojo.com/api/user_interface/web/webcanvas.html#webcanvas-paint" target="_blank" rel="noreferrer noopener">Paint</a> event is passed a <a href="https://documentation.xojo.com/api/graphics/webgraphics.html" target="_blank" rel="noreferrer noopener">WebGraphics</a> object. While the Graphics and WebGraphics classes have a lot in common, sharing many of the same methods with the same parameters, they are two distinctly different classes from the perspective of the Xojo compiler. That means that while the code you write will look almost identical, you still have to use a Graphics object for Android, Desktop and iOS projects and a WebGraphics object for Web projects. </p>



<p>Your instinct might be to simply write the code for one and then copy it to the other projects, changing Graphics to WebGraphics for Web projects. This will of course work, but then you have to remember to do this copy/paste/change operation every time you update your code. That&#8217;s not ideal.</p>



<h2 class="wp-block-heading">Graphics vs. WebGraphics</h2>



<p>With Xojo, the code can be written in such a way as to automatically handle the difference between Graphics/WebGraphics. Most often your graphics methods will be called from something that provides a Graphics or WebGraphics object for you to draw into such as a Canvas control&#8217;s Paint event. From there your code will call your methods that do the drawing. The problem is that your methods can&#8217;t use a Graphics or a WebGraphics type as a parameter because Graphics is not going to work for your Web projects and WebGraphics doesn&#8217;t exist in any framework except the Web framework. Thus using it anywhere else will result in a compiler error.</p>



<h3 class="wp-block-heading">Using a Variant</h3>



<p>Getting around this limitation is, fortunately, quite easy. Instead of using Graphics or WebGraphics as a parameter type, you use <a href="https://documentation.xojo.com/api/data_types/variant.html" target="_blank" rel="noreferrer noopener">Variant</a>. A Variant can hold any type of data from Strings to Integers, to Graphics to WebGraphics and more.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="560" height="466" src="https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-14.46.40@2x.png" alt="" class="wp-image-14827" srcset="https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-14.46.40@2x.png 560w, https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-14.46.40@2x-300x250.png 300w" sizes="(max-width: 560px) 100vw, 560px" /></figure>



<p>Once your drawing method gets the Variant though, it can&#8217;t use it to draw. It can only use a Graphics or WebGraphics objects. The trick is to then copy it into a Graphics or WebGraphics class variable:</p>



<pre class="wp-block-code"><code><code>Var g As Graphics = context</code></code></pre>



<p>or</p>



<pre class="wp-block-code"><code><code>Var g As WebGraphics = context</code></code></pre>



<p>The problem you now face, as mentioned earlier, is that WebGraphics only exists in Web projects. That means you have to write your code in such a way that you will declare the variable g as a Graphics object in Android, Desktop and iOS projects and as a WebGraphics object in Web projects. This can be achieved via conditional compilation. This is a simple technique that tells the Xojo compiler to only compile code into your app if conditions you specify are met. In this case, if you&#8217;re targeting Desktop or Mobile, it should compile in the line that uses the Graphics type, and if you are targeting a Web app, the line that uses WebGraphics. To conditionally compile, you just need to add that conditional code around these two lines:</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code"><code>#If TargetDesktop Or TargetMobile Then
   Var g As Graphics = context
#EndIf

#If TargetWeb Then
   Var g As WebGraphics = context
#EndIf</code></pre>
</div></div>
</div></div>



<p>Since a project must be only one of the above types, when you run or build your project, the appropriate line of code will be compiled in. From there, you now have a variable (g) of the right type can use this to draw whatever you like. For example, there are four identical example projects (one for each project type) that show off this technique. They can be found in the Xojo IDE by choosing File > New Project, clicking on Examples, choosing the Graphics folder in the list and then the Cross-Platform Drawing folder. The examples all draw a checkerboard that looks like this:</p>


<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><img decoding="async" width="912" height="916" src="https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-15.12.41@2x.png" alt="" class="wp-image-14828" style="width:315px;height:auto" srcset="https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-15.12.41@2x.png 912w, https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-15.12.41@2x-300x300.png 300w, https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-15.12.41@2x-150x150.png 150w, https://blog.xojo.com/wp-content/uploads/2025/04/CleanShot-2025-04-17-at-15.12.41@2x-768x771.png 768w" sizes="(max-width: 912px) 100vw, 912px" /></figure>
</div>


<p>They all contain the an identical Checkerboard class that handles the drawing. This class has DrawBorder and DrawCheckers methods that both take a Variant as a parameter. In each project is a Canvas control whose Paint event calls these methods:</p>



<pre class="wp-block-code"><code>Var cb As New Checkerboard
cb.DrawBoard(g)
cb.DrawCheckers(g)</code></pre>



<p>The full DrawBoard method looks like this:</p>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code"><code>#If TargetDesktop Or TargetMobile Then
  Var g As Graphics = context
#EndIf

#If TargetWeb Then
  Var g As WebGraphics = context
#EndIf 

Var boxSize As Integer = Min(g.Width/8, g.Height/8) 'The size of the box
'Draw the rows
For y As Integer = 0 To 7
  For x As Integer = 0 To 7
    'Switch colors for each box in the row
    If g.DrawingColor = Color.Black Then
      g.DrawingColor = Color.White
    Else
      g.DrawingColor = Color.Black
    End If
    'Draw a box
    g.FillRectangle(x * boxSize, y * boxSize, boxSize, boxSize)
  Next
  'Switch colors again for the beginning of the next row
  If g.DrawingColor = Color.Black Then
    g.DrawingColor = Color.White
  Else
    g.DrawingColor = Color.Black
  End If
Next</code></pre>
</div></div>



<p>In the example projects, check out the DrawCheckers method as well. It uses the same technique. What is great about this is that this class can be copied into any Android, Desktop, iOS or Web project and it will indeed <em>just work</em>.</p>



<h3 class="wp-block-heading">Checking Out the Examples</h3>



<p>There are four example projects (one for each project type &#8211; Android, Desktop, iOS and Web) that demonstrate the technique explained here. You can find them in the Xojo IDE by choosing File > New Project, clicking on Examples, choosing the Graphics folder in the list and then the Cross-Platform Drawing folder.</p>



<p><em>Geoff Perlman is the Founder and CEO of Xojo. When he’s not leading the Xojo team he can be found playing drums in Austin, Texas and spending time with his family.</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>Everything You Need to Know About Variants in Xojo</title>
		<link>https://blog.xojo.com/2024/10/10/everything-you-need-to-know-about-variants-in-xojo/</link>
		
		<dc:creator><![CDATA[Martin T.]]></dc:creator>
		<pubDate>Thu, 10 Oct 2024 16:45:00 +0000</pubDate>
				<category><![CDATA[Guest Post]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Beginner Tips]]></category>
		<category><![CDATA[Data Type]]></category>
		<category><![CDATA[Variants]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=13893</guid>

					<description><![CDATA[One of the most interesting features in Xojo is the Variant data type. We will explore the so-called “Implicit Conversions,” discuss the pros and cons&#8230;]]></description>
										<content:encoded><![CDATA[
<p>One of the most interesting features in Xojo is the Variant data type. We will explore the so-called “Implicit Conversions,” discuss the pros and cons of using Variants, and analyze when it makes sense to use this data type.</p>



<p>For more detailed information, you can refer to the official Xojo documentation on <a href="https://documentation.xojo.com/api/data_types/variant.html" target="_blank" rel="noreferrer noopener">Variants</a>.</p>



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



<p>A Variant in Xojo is a data type that can store any type of data. This means that a variable of type Variant can take on different data types without needing to be explicitly converted. This can be very useful when you want to be flexible, but it can also lead to unexpected problems if not handled correctly.</p>



<p>Basic Example</p>



<pre class="wp-block-code"><code>Var dynamicValue As Variant

dynamicValue = 42                              // Integer
dynamicValue = "Hello"                         // String
dynamicValue = 3.14                            // Double
dynamicValue = True                            // Boolean
dynamicValue = Color.RGB(255, 0, 0)            // Color
dynamicValue = New Dictionary("key" : "value") // Dictionary</code></pre>



<p>In the example above, you can see that dynamicValue can take on different data types without the need to explicitly change the type.</p>



<h2 class="wp-block-heading">Implicit Conversions</h2>



<p>Implicit Conversions are automatic type conversions performed by Xojo when using Variants. This means that Xojo tries to interpret the data in a Variant so that it fits the context in which it is used.</p>



<h3 class="wp-block-heading">Example of an Implicit Conversion</h3>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = "123"

Var myInteger As Integer
myInteger = dynamicValue  // Implicit conversion from String to Integer</code></pre>



<p>In this example, the string “123” is automatically converted to an integer when assigned to the integer variable myInteger.</p>



<h3 class="wp-block-heading">Another Example with Data Types</h3>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = "3.14159"

Var myDouble As Double
myDouble = dynamicValue  // Implicit conversion from String to Double</code></pre>



<p>Here, the string “3.14159” is automatically converted to a double. This demonstrates the flexibility of Variants and Xojo’s ability to handle different data types.</p>



<h2 class="wp-block-heading">Conversion of Floating-Point Numbers</h2>



<p>In Xojo, assigning a floating-point number to a Variant will always produce a Double. However, there are other floating-point data types such as Single, Currency, and CGFloat.</p>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = 3.14

Var myDouble As Double
Var mySingle As Single
Var myCurrency As Currency
Var myCGFloat As CGFloat

myDouble = dynamicValue   // Double: 3.14
mySingle = dynamicValue   // Single 3.14 (implicitly converted)
myCurrency = dynamicValue // Currency: 3.14 (implicitly converted)
myCGFloat = dynamicValue  // CGFloat: 3.14 (implicitly converted)</code></pre>



<p>In this example, you can see that assigning dynamicValue (which contains a floating-point number) to different floating-point data types always works, with the implicit conversion performed by Xojo.</p>



<h2 class="wp-block-heading">Advantages of Implicit Conversions</h2>



<p></p>



<h3 class="wp-block-heading">Flexibility</h3>



<p>The biggest advantage of Variants is their flexibility. You can write functions that can work with different data types without requiring explicit type conversion.</p>



<h3 class="wp-block-heading">Example of a Flexible Function</h3>



<pre class="wp-block-code"><code>Function AddValues(value1 As Variant, value2 As Variant) As Variant
  Return value1 + value2
End Function

// Using the function with different data types
Var result As Variant
result = AddValues(10, 20)             // Integer addition
result = AddValues("Hello", " World")  // String concatenation
result = AddValues(3.14, 2.71)         // Double addition</code></pre>



<p>This function can work with integers, strings, and doubles without requiring changes to the code.</p>



<h3 class="wp-block-heading">Rapid Prototyping</h3>



<p>During rapid prototyping, Variants can be useful because they reduce the need to worry about type conversions.</p>



<h3 class="wp-block-heading">Example of a Quick Prototype</h3>



<pre class="wp-block-code"><code>Var quickData As Variant = "42"

If quickData &gt; 10 Then
  MessageBox("Greater than 10")
End If</code></pre>



<p>In this example, the string “42” is automatically converted to an integer to perform the comparison.</p>



<h2 class="wp-block-heading">Disadvantages of Implicit Conversions</h2>



<p></p>



<h3 class="wp-block-heading">Debugging</h3>



<p>It can be difficult to debug errors caused by incorrect type conversions. Since the conversion happens automatically, it is sometimes not obvious why a particular error occurs.</p>



<h3 class="wp-block-heading">Performance Issues</h3>



<p>Implicit conversions can lead to performance issues, especially when large amounts of data need to be converted.</p>



<h3 class="wp-block-heading">Example of Performance Issues</h3>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = "9999999999"
Var myDouble As Double

For i As Integer = 1 To 1000000
  myDouble = dynamicValue  // Performance hit due to repeated conversions
Next</code></pre>



<p>In this example, the conversion is performed during each iteration of the loop, which can impact performance.</p>



<h3 class="wp-block-heading">Loss of Type Safety</h3>



<p>Another risk is the loss of type safety, which can lead to unexpected runtime errors.</p>



<h2 class="wp-block-heading">Type Property</h2>



<p>The Type property is a useful tool to determine the data type of a Variant value. This property returns an integer that corresponds to the type of the value. This can be helpful to ensure that you are handling the correct data type.</p>



<h3 class="wp-block-heading">Example of Using the Type Property</h3>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = "Hello"

Select Case dynamicValue.Type
Case Variant.TypeString
  MessageBox("The variant is a String.")
Case Variant.TypeInt32, _
     Variant.TypeInt64
  MessageBox("The variant is an Integer.")
Case Variant.TypeDouble
  MessageBox("The variant is a Double.")
Case Variant.TypeBoolean
  MessageBox("The variant is a Boolean.")
Else
  MessageBox("The variant is of another type.")
End Select</code></pre>



<p>In this example, dynamicValue.Type is used to determine the type of the Variant value and perform corresponding actions.</p>



<h2 class="wp-block-heading">When Should You Use Variants? </h2>



<p></p>



<h3 class="wp-block-heading">Suitable Use Cases</h3>



<p></p>



<h3 class="wp-block-heading">Generic Functions</h3>



<p>If you are writing functions that need to work with different data types, Variants can be very useful.</p>



<pre class="wp-block-code"><code>Function CompareValues(value1 As Variant, value2 As Variant) As Boolean
  Return value1 = value2
End Function

// Using the function with different data types
Var isEqual As Boolean
isEqual = CompareValues(10, 10)          // True
isEqual = CompareValues("Hello", "Hello")  // True
isEqual = CompareValues(3.14, 2.71)      // False</code></pre>



<h3 class="wp-block-heading">Interoperability with External APIs</h3>



<p>When working with APIs or interfaces that return different data types, Variants can provide an easy way to handle this data.</p>



<pre class="wp-block-code"><code>// Example API response that can return different data types
Function GetApiResponse() As Variant
  // Simulated API response
  Return "42"  // Could also be an Integer or a JSON object
End Function

Var apiResponse As Variant = GetApiResponse

If apiResponse.Type = Variant.TypeString Then
  MessageBox("Response is a string: " + apiResponse)
ElseIf apiResponse.Type = Variant.TypeInt32 Or _
       apiResponse.Type = Variant.TypeInt64 Then
  MessageBox("Response is an integer: " + apiResponse)
End If</code></pre>



<h3 class="wp-block-heading">Data Structures</h3>



<p>In situations where you need data structures that contain different data types (e.g., an array that can contain both strings and numbers), Variants are a good choice.</p>



<pre class="wp-block-code"><code>Var dataList() As Variant
dataList.Add("Xojo")
dataList.Add(2024)
dataList.Add(True)

// Accessing different data types in the array
For Each item As Variant In dataList
  Select Case item.Type
  Case Variant.TypeString
    MessageBox("String: " + item) 
  Case Variant.TypeInt32, _
       Variant.TypeInt64
    MessageBox("Integer: " + item) 
  Case Variant.TypeBoolean
    MessageBox("Boolean: " + item)
  End Select
Next</code></pre>



<h2 class="wp-block-heading">Situations to Avoid Using Variants</h2>



<p></p>



<h3 class="wp-block-heading">Performance-Critical Applications</h3>



<p>When performance is critical, you should avoid Variants because automatic type conversion introduces additional overhead.</p>



<pre class="wp-block-code"><code>Var dynamicValue As Variant = "9999999999"
Var myDouble As Double

For i As Integer = 1 To 1000000
  myDouble = dynamicValue  // Performance hit due to repeated conversions
Next</code></pre>



<h3 class="wp-block-heading">Code Clarity and Maintainability</h3>



<p>When code readability and maintainability are paramount, you should use explicit data types to ensure that the code is understandable to other developers.</p>



<pre class="wp-block-code"><code>Var myString As String = "Hello"
Var myInteger As Integer = 123

Function ConcatenateStrings(s1 As String, s2 As String) As String
  Return s1 + s2
End Function

Var result As String
result = ConcatenateStrings(myString, myInteger.ToString)  // Clear and understandable</code></pre>



<p>The use of Variants in Xojo offers both advantages and disadvantages. While flexibility and rapid prototyping capabilities are compelling arguments, you should be aware of the potential problems that can arise from implicit conversions. Variants are best suited for scenarios where flexibility and generic programming are required. In performance-critical applications or projects that require high maintainability, it is advisable to use explicit data types such as String, Integer, Double, Boolean, Color etc. or, if you decide to use Variant, you can also have the explicit data type of the Variant delivered. To do this, use the Variant &#8230;Value properties (for example StringValue, IntegerValue, BooleanValue etc.).</p>



<p>By taking a conscious and deliberate approach to using Variants, you can leverage their advantages while avoiding potential pitfalls.</p>



<p>For more detailed information, you can refer to the official Xojo documentation on <a href="https://documentation.xojo.com/api/data_types/variant.html">https://documentation.xojo.com/api/data_types/variant.html</a>.</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>
	</channel>
</rss>
