# Conversion from a Variant to a subclass through return is not done and does a RuntimeError

**URL:** <https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827>\
**Category:** Bugs and Feature Requests\
**Created:** [June 26, 2024, 9:43pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827 "2024-06-26T21:43:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 26, 2024, 9:43pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/1 "2024-06-26T21:43:29Z")

</div>

I have a problem.

When using the Return statement, the conversion of a subclass of an empty array doesn’t work.

```auto
Function Test_Array() As cSuperClass()
  Var vObjects() As cSubClass
  Var vVariant As Variant
  
  vVariant = vObjects // Prove you can convert an array of objects to a Variant
  vObjects = vVariant // Prove you can convert Variant containing an array of objects to the original objects’ array
  
  Return vObjects // works
  Return vVariant // doesn’t work
End Function

```

But if I do the same with just an object, all works well…

```auto
Function Test_Object() As cSuperClass
  Var vObject As cSubClass
  Var vVariant As Variant
  
  vVariant = vObject
  vObject = vVariant
  
  Return vObject // works
  Return vVariant // works
End Function

```

This limitation is written no where in the doc. Is it a bug, as I am assuming?

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 26, 2024, 10:03pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/2 "2024-06-26T22:03:33Z")

</div>

In the second example, Xojo can test the variant’s value directly and see that it is an object of type cSubClass, which is a subclass of cSuperClass and thus can be returned.

In the first example, Xojo looks at what you are trying to return and sees a single object of type Variant. This doesn’t match the return signature (array of object cSuperClass) and so you get a TypeMismatchException. Xojo isn’t willing to peek inside the variant itself to see what it represents.

Arguably, it should be able/willing to do this, but it isn’t. For other possible values inside variants, like integers and strings, the variant object provides automatic conversions between those types and so you can use them as return values in all sorts of functions. There (apparently) is no automatic conversion to arrays and so you get the exception. I venture to guess this is because of Xojo’s strict type constraints that it can’t or won’t provide the appropriate conversion mechanism.

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 26, 2024, 10:10pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/3 "2024-06-26T22:10:46Z")

</div>

> [@Eric\_Williams](#):
>
> I venture to guess this is because of Xojo’s strict type constraints that it can’t or won’t provide the appropriate conversion mechanism.

Can we agree that an object subclass IsA object superclass?

This logic should also apply to Arrays of…

But I understand that an Array is itself, in Xojo, also an object of a class Array, and it would require more code to confirm that the array IsA subclass of the array returned…

I now see that the problem is not only with the `Return` statement, but also in code…

 ![image](https://forum.xojo.com/uploads/default/original/3X/0/1/01a36b36f6fceb3948aa83aca8bc7008864d9215.png)

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 26, 2024, 10:15pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/4 "2024-06-26T22:15:41Z")

</div>

Yep - you can’t do that.

You’ve wandered into the dark corner of the basement where Variant and Array both live. I generally avoid Variant unless absolutely necessary, because things can be going along just great until you run into one of these weird situations where Variant won’t **quite** give you what you need, and you end up restructuring a bunch of code. I’ve generally found it better to not use them if possible.

One clue that things are about to go wrong is if you try to use Variant to get around Xojo’s strict type checking. It almost always comes back to bite you.

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 26, 2024, 10:30pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/5 "2024-06-26T22:30:36Z")

</div>

> [@Eric\_Williams](#):
>
> I generally avoid Variant unless absolutely necessary, because things can be going along just great until you run into one of these weird situations where Variant won’t **quite** give you what you need, and you end up restructuring a bunch of code. I’ve generally found it better to not use them if possible.

Well, until this special case, Variant were working perfectly for me.  
It’s just the special case where I convert an array as Variant to an array of the super class.

Since I have more than 100 subclasses of that particular type, it would be complex for me to bypass that. Not impossible, I can write my own Array object… But since nowhere in the documentation it’s written that Variant have this limitation, I hope it’s just an oversight that they will fix in, let say, v2024r3? 😉

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 27, 2024, 12:59am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/6 "2024-06-27T00:59:56Z")

</div>

I’m not sure that this is a bug, exactly, so it’s hard to say how urgent Xojo considers it. Perhaps you could search the issue tracker to see if anyone else has filed an issue and sign on to it.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [June 27, 2024, 1:28am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/7 "2024-06-27T01:28:13Z")

</div>

It’s also possible to have your class behave _like_ an array without being an array.

For example, your class can implement some of the array-like behaviors such as:  
**Operator\_Subscript**  
[https://documentation.xojo.com/api/math/operator\_subscript.html](https://documentation.xojo.com/api/math/operator_subscript.html)

… and you can write other methods such as `.Count` , `.addRow` , `IndexOf()` etc. as needed.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 27, 2024, 4:03am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/8 "2024-06-27T04:03:12Z")

</div>

Let’s take a step back and look at the larger picture. You’ve got a SuperClass, and over 100 subclasses. You’re running into this Variant/Array problem. What are you actually trying to accomplish? Maybe there’s a way to do it that doesn’t involve Variants.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [June 27, 2024, 4:09am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/9 "2024-06-27T04:09:15Z")

</div>

> [@Antoine\_Beaubien](#):
>
> Since I have more than 100 subclasses of that particular type,

“Favor composition over inheritance” is **a design principle that suggests it’s better to compose objects to achieve polymorphic behavior and code reuse rather than inheriting from a base class**.

Just saying…

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 27, 2024, 5:12am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/10 "2024-06-27T05:12:00Z")

</div>

Yes. In my project, I already have 3 arrays classes, and I dont even use the **Operator\_Subscript**. I just expose the array in a method with ByRef parameter. It spares me from overloading a lot of methods.

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 27, 2024, 5:14am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/11 "2024-06-27T05:14:39Z")

</div>

> [@Eric\_Williams](#):
>
> Let’s take a step back and look at the larger picture. You’ve got a SuperClass, and over 100 subclasses. You’re running into this Variant/Array problem. What are you actually trying to accomplish? Maybe there’s a way to do it that doesn’t involve Variants.

I have an API, and it’s generic, so it returns the data as a Variant. It can be a boolean, a string, a number, or a subclass of my generic object.

I already a way to compensate, and it’s to do my own array of object class.

By it’s basically rewriting a core fonctionalty of the language, Arrays, just because a rare but simple case is not handled.

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 27, 2024, 5:37am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/12 "2024-06-27T05:37:49Z")

</div>

> [@Beatrix\_Willius](#):
>
> “Favor composition over inheritance” is **a design principle that suggests it’s better to compose objects to achieve polymorphic behavior and code reuse rather than inheriting from a base class**.

Not really an option.

Each subclass have it’s property defined as Setter/Getter, and also have some commands.  
It would be too generic.

Subclassing does give me the right mix of genericity and personnality.  
And it exists for that. Why not use it? And it’s not like I’m abusing the concept.

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [June 27, 2024, 8:07am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/13 "2024-06-27T08:07:03Z")

</div>

> [@Antoine\_Beaubien](#):
>
> `Test_Array() As cSuperClass()`

I see Syntax Errors.

The Manual says:

> Only one-dimensional arrays are supported by Array. Higher dimensional arrays can be populated via assignment statements.

An Array of Arrays does not sound like a one-dimensional array. 😉

---

<div class="post-metadata">

**Author:** ![Antoine\_Beaubien](https://forum.xojo.com/user_avatar/forum.xojo.com/antoine_beaubien/32/12603_2.png) [@Antoine\_Beaubien](https://forum.xojo.com/u/Antoine_Beaubien)\
**Post date:** [June 27, 2024, 8:46am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/14 "2024-06-27T08:46:43Z")

</div>

> [@Sascha\_S](#):
>
> An Array of Arrays does not sound like a one-dimensional array. 😉

`Test_Array() As cSuperClass` is one dimension array.

Regards, Antoine

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 27, 2024, 11:50am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/15 "2024-06-27T11:50:33Z")

</div>

It is a one-dimensional array of array objects.

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [June 27, 2024, 11:59am UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/16 "2024-06-27T11:59:52Z")

</div>

> [@Eric\_Williams](#):
>
> a one-dimensional array of array objects

> A _one-dimensional array_ (or _array_) is a data structure that stores a sequence of (references to) objects. We refer to the objects within an array as its _elements_. The method that we use to refer to elements in an array is _numbering_ and then _indexing_ them. If we have _n_ elements in the sequence, we think of them as being numbered from 0 to _n_ - 1. Then, we can unambiguously specify one of them by referring to the _i_th element for any integer _i_ in this range.
> 
> **A _two-dimensional array_ is an array of (references to) one-dimensional arrays.** Whereas the elements of a one-dimensional array are indexed by a single integer, the elements of a two-dimensional array are indexed by a pair of integers: the first specifying a row, and the second specifying a column.

Source: [Arrays](https://introcs.cs.princeton.edu/python/14array/)

Ho would i target Index Y of Element X in such an Array?  
`If Array(2, 3) <> ...` ← Looks like a multidimensional array to me.  
Would I always have to reference or copy the array element into a new array in order to access its index? Doesn’t sound very effective.

Please excuse me, but I really don’t understand. 🤷

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [June 27, 2024, 12:57pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/17 "2024-06-27T12:57:52Z")

</div>

```auto
Dim aBunchOfArrays() as variant

Dim oneArray() as string

oneArray.Add "banana"
oneArray.Add "orange"
oneArray.Add "apple"

aBunchOfArrays.Add oneArray

Dim anotherArray() as string

anotherArray.Add "cat"
anotherArray.Add "dog"
anotherArray.Add "llama"

aBunchOfArrays.Add anotherArray

dim someArray() as string

someArray=aBunchOfArrays(1)

//someArray(1) will be "dog"

someArray=aBunchOfArrays(0)

//someArray(0) will be "banana'

```

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [June 27, 2024, 1:11pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/18 "2024-06-27T13:11:28Z")

</div>

> [@Eric\_Williams](#):
>
> ```auto
> dim someArray() as string
> someArray=aBunchOfArrays(1)
> //someArray(1) will be "dog"
> someArray=aBunchOfArrays(0)
> //someArray(0) will be "banana'
> 
> ```

So, as expected, you would have to reference each array again just to be able to access elements of this array. That sounds ineffective. 🫥  
I’m sure one or two people would have a use for it… but it’s not for me.

I’m still convinced that we’re talking about multidimensional arrays. 😉

Thank you very much for your helpful explanation. 👍

---

<div class="post-metadata">

**Author:** ![Thomas\_Kaltschmidt1](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/e79b87/32.png) [@Thomas\_Kaltschmidt1](https://forum.xojo.com/u/Thomas_Kaltschmidt1)\
**Post date:** [June 27, 2024, 1:29pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/19 "2024-06-27T13:29:41Z")

</div>

Hm, but `aBunchOfArrays()` is of VarType `Object`, not `Array`

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [June 27, 2024, 1:35pm UTC](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827/20 "2024-06-27T13:35:26Z")

</div>

That’s correct:

```auto
Arrays are treated as objects so be aware that assigning an array to another variable or passing an array as a parameter does not make a copy of the array.Instead you get a new variable that points to the original array so any changes you make to the "2nd" array also affects the first array. If you need an actual copy of the array you will have to manually copy its values using a loop.

```

Source: [Arrays — Xojo documentation](https://documentation.xojo.com/api/language/arrays.html#arrays)

[Next page](https://forum.xojo.com/t/conversion-from-a-variant-to-a-subclass-through-return-is-not-done-and-does-a-runtimeerror/80827.md?page=2)
