# Auto vs. Variant, when to use Auto?

**URL:** <https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169>\
**Category:** General\
**Created:** [May 11, 2015, 3:50am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169 "2015-05-11T03:50:54Z")\
**Posts on this page:** 8\
**Page:** 5

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [August 17, 2015, 8:33am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/81 "2015-08-17T08:33:50Z")

</div>

The general concept of the auto data type is good and the right way. Using introspection to determine type information  
is no problem but in terms of performance it could be tweaked a bit.

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 17, 2015, 9:00am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/82 "2015-08-17T09:00:44Z")

</div>

> [@207494:@Rob Egal](#):
>
> but in terms of performance it could be tweaked a bit.

It would be great if that was built in. The Variant data type had a good and a bad side:

The good was that you could query it for the data type it was holding in a fast way.

The bad that you were able to assign the value to whatever data type you wanted (causing hard to find bugs due to having no compile time checks).

Unfortunately Xojo now leaves the good part of Variant up to you too. I created a module called “Auto” with extension methods to exactly mimic the Variant Type method and the TypeInteger, TypeBoolean, etc. constants. But used in loops it shows to be a lot slower than Variant’s Type method.

---

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [August 17, 2015, 11:25am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/83 "2015-08-17T11:25:14Z")

</div>

[quote=207498:@Eli Ott]It would be great if that was built in. The Variant data type had a good and a bad side:

The good was that you could query it for the data type it was holding in a fast way.

The bad that you were able to assign the value to whatever data type you wanted (causing hard to find bugs due to having no compile time checks).

Unfortunately Xojo now leaves the good part of Variant up to you too. I created a module called “Auto” with extension methods to exactly mimic the Variant Type method and the TypeInteger, TypeBoolean, etc. constants. But used in loops it shows to be a lot slower than Variant’s Type method.[/quote]

To be honest I don’t see it built in but let’s see what the future brings…

---

<div class="post-metadata">

**Author:** ![Joe\_Ranieri](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@Joe\_Ranieri](https://forum.xojo.com/u/Joe_Ranieri)\
**Post date:** [August 17, 2015, 12:42pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/84 "2015-08-17T12:42:17Z")

</div>

> [@207513:@Rob Egal](#):
>
> To be honest I don’t see it built in but let’s see what the future brings…

The eventual idea will be that you will be able to do something like:

`Select Case Xojo.Introspection.GetType(myAuto)
Case GetTypeInfo(Integer)
// stuff
Case GetTypeInfo(Text)
// other stuff
End Select`

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 17, 2015, 12:50pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/85 "2015-08-17T12:50:55Z")

</div>

It is slow (it involves string comparison).  
It is error-prone.  
Each developer has to re-invent the wheel.

[code] Dim typeInfo As TypeInfo = GetType(value)

If typeInfo Is Nil Then  
// Nil

ElseIf typeInfo.IsPrimitive Then  
Select Case typeInfo.Name  
Case “Boolean”  
// Boolean  
Case “Color”  
// Color  
Case “Int8”, “Int16”, “Int32”, “UInt8”, “UInt16”, “UInt32”  
// Integer  
Case “Int64”, “UInt64”  
// Long  
Case “Single”  
// Single  
Case “Double”  
// Double  
Case “Currency”  
// Currency  
Case “Text”  
// Text  
Case “Ptr”  
// Ptr  
Case “CString”  
// CString  
Else  
… // TODO  
End

ElseIf typeInfo.IsValueType Then  
// Structure  
// This is just a guess, as there is no TypeInfo.IsStructure property

ElseIf typeInfo.IsClass Then  
If typeInfo.Name = “Xojo.Core.Date” Then  
// Date  
Else  
// Object, but not a Xojo.Core.Date  
End

ElseIf typeInfo.IsArray Then  
Select Case typeInfo.ArrayElementType.Name  
Case “Boolean”  
// Array of Booleans  
Case “Color”  
// Array of Colors  
…  
and so on, you get the picture  
…  
End

Else  
// more?

End[/code]

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [August 17, 2015, 1:17pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/86 "2015-08-17T13:17:52Z")

</div>

[quote=207523:@Joe Ranieri]The eventual idea will be that you will be able to do something like:

`Select Case Xojo.Introspection.GetType(myAuto)
Case GetTypeInfo(Integer)
// stuff
Case GetTypeInfo(Text)
// other stuff
End Select`[/quote]

Wouldn’t it be better if Xojo just did an Auto.Type method then? I mean we could always build an extension method but it seems silly to have the user have to use Xojo.Introspection.GetType for something so key to using Auto.

`Select Case myAuto.Type
Case GetTypeInfo(Integer)`

---

<div class="post-metadata">

**Author:** ![Rob\_Egal](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/76d3ee/32.png) [@Rob\_Egal](https://forum.xojo.com/u/Rob_Egal)\
**Post date:** [August 17, 2015, 1:34pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/87 "2015-08-17T13:34:34Z")

</div>

[quote=207523:@Joe Ranieri]The eventual idea will be that you will be able to do something like:

`Select Case Xojo.Introspection.GetType(myAuto)
Case GetTypeInfo(Integer)
// stuff
Case GetTypeInfo(Text)
// other stuff
End Select`[/quote]

That’s good so far. I didn’t mean it in any other way.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:45pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/88 "2020-10-29T17:45:01Z")

</div>



[Previous page](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169.md?page=4)
