# Object Array shenanigans

**URL:** <https://forum.xojo.com/t/object-array-shenanigans/65497>\
**Category:** General\
**Created:** [September 10, 2021, 9:16pm UTC](https://forum.xojo.com/t/object-array-shenanigans/65497 "2021-09-10T21:16:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 10, 2021, 9:16pm UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/1 "2021-09-10T21:16:10Z")

</div>

I have a shared method into which I pass arrays of controls (mix of rectControls and containerControls) to effect alignment. I was previously using variant arrays and sometimes had problems when there was only one type of item in the array - the compiler would then seem to auto format as a array of the item’s type, rather than a variant array.

So instead I though I’d use an Object() array, thinking this would fix the problem as all the controls are subclasses of objects.

```auto
Dim objs() As object 
objs = Array(tbClosed, eDueDate)
//where tbClosed is a canvas subclass and
//eDueDate is a containterControl subclass

```

But now the compiler gives an error stating “Type mismatch error. Expected Object() but got Variant().” I suppose this is the compiler doing its thing again.

So I tested the alternative of:

```auto
Redim objs(-1)
objs.append tbClosed
objs.append eDueDate

```

This doesn’t give a compile error but would require a significant amount of re-coding on all the controls I have (100’s of controls).

Any ideas?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 11, 2021, 5:42am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/2 "2021-09-11T05:42:49Z")

</div>

Instead of object, use variant for the array declarations.  
Same thing, but less hassle.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [September 11, 2021, 8:50am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/3 "2021-09-11T08:50:43Z")

</div>

That is normal for array function.  
But you can put first value into variant first to make sure you get that type.

---

<div class="post-metadata">

**Author:** ![Sebastien\_REMY](https://forum.xojo.com/user_avatar/forum.xojo.com/sebastien_remy/32/1946_2.png) [@Sebastien\_REMY](https://forum.xojo.com/u/Sebastien_REMY)\
**Post date:** [September 11, 2021, 10:41am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/4 "2021-09-11T10:41:39Z")

</div>

> [@James\_Pitchford](#):
>
> This doesn’t give a compile error but would require a significant amount of re-coding on all the controls I have (100’s of controls).

DRY Don’t Repeat Yourself is not about coding but maintenance: perfect example. You may subclass your controls…

Have a nice day 😀

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [September 11, 2021, 10:54am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/5 "2021-09-11T10:54:52Z")

</div>

> [@James\_Pitchford](#):
>
> I have a shared method into which I pass arrays of controls (mix of rectControls and containerControls) to effect alignment. I was previously using variant arrays and sometimes had problems when there was only one type of item in the array - the compiler would then seem to auto format as a array of the item’s type, rather than a variant array.

Perhaps the easiest workaround, instead of rewriting your whole method, could be to add a dummy object, like:

Var dt as DateTime=DateTime.Now  
Var MyArray() As Variant=Array(dt,PushButton1,CheckBox1)

That way you guarantee the array is never converted; you just skip the first item when dealing with the array. Note I’m note sure this syntax is accepted (not in front of Xojo right now). You may try appending “dt” later, if it works better.

Yes, ugly, but still a workaround (for a problem that shouldn’t be there in the first place, IMO).

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [September 11, 2021, 2:36pm UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/6 "2021-09-11T14:36:32Z")

</div>

While I have never used it with variants, I have found the array function frustrating because it’s assumptions about integer types…

One way is to have have the compiler look at what the array is being assigned to determine array type instead of the first element…

Another way of doing this for situations like these in a more verbose API2ish way could be something like:

Dim theArray() as Datatype = DataType.Array(Item1, item2, Item3, …)

Dim Uint8Array() as Unit8 = Uint8.Array(5,10,15)  
Dim Int32() as Int32 = Int32.Array(5,10,15)  
Dim VarArray() As Variant = Variant.Array(Item1, item2, Item3, …)

And for this syntax, even though the current function is fine in this case, just to be consistent maybe also support:  
Dim MyClassArray() as MyClass = MyClass.Array(Inst1, Inst2, Inst2)  
Dim MyStructArray() as MyStruct = MyStruct.Array(item1, Item2, item3)

-Karen

---

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 12, 2021, 12:19am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/7 "2021-09-12T00:19:02Z")

</div>

> [@KarenA](#):
>
> Dim VarArray() As Variant = Variant.Array(Item1, item2, Item3, …)

Karen - this looked good, but gives a syntax error. ??

> [@Christian\_Schmitz](#):
>
> But you can put first value into variant first to make sure you get that type.

This did work thanks Christian. My example code becomes:

```auto
objs = Array(variant(tbClosed), eDueDate)

```

A bit ugly but saves a heavy re-write.

> [@Sebastien\_REMY](#):
>
> DRY Don’t Repeat Yourself is not about coding but maintenance: perfect example. You may subclass your controls…

Thanks Sebastien. Yes the controls are all subclassed already. But I have them in varying groups and alignments dependent upon other selections.

---

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 12, 2021, 12:21am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/8 "2021-09-12T00:21:10Z")

</div>

> [@Arnaud\_N](#):
>
> Var MyArray() As Variant=Array(dt,PushButton1,CheckBox1)

I also tried this Arnaud, but each time I compile a different array into the variable it fails again, despite the type of the array when originally dim’d (var’d?).

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [September 12, 2021, 12:24am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/9 "2021-09-12T00:24:13Z")

</div>

> [@James\_Pitchford](#):
>
> > [@KarenA](#):
> >
> > Dim VarArray() As Variant = Variant.Array(Item1, item2, Item3, …)
> 
> Karen - this looked good, but gives a syntax error. ??

That was one way they COULD implement that as a feature if they wanted to… It does not exist now.

-karen

---

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 12, 2021, 12:25am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/10 "2021-09-12T00:25:22Z")

</div>

Ah, understand. Yes that would be useful.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [September 12, 2021, 12:28am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/11 "2021-09-12T00:28:23Z")

</div>

> [@James\_Pitchford](#):
>
> each time I compile a different array into the variable it fails again, despite the type of the array when originally dim’d (var’d?)

That’s because you’re throwing away the array you just dim’d and replacing it with an entirely new array which could be of any type.

---

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 12, 2021, 12:36am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/12 "2021-09-12T00:36:55Z")

</div>

> [@James\_Pitchford](#):
>
> This did work thanks Christian. My example code becomes:

Ahem, maybe I spoke too soon. My initial tests worked but subsequent tests don’t always. See code:

```auto

Dim cc As New ContainerControl
Dim cv As New canvas

Dim v1() As Variant
v1 = Array(cc, cv) //works

Dim v2() As Variant
v2 = Array( Variant(cc), cv) //type mismatch

```

Oh for some consistency!!

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [September 12, 2021, 2:09am UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/13 "2021-09-12T02:09:29Z")

</div>

Perhaps for the sake of your existing code, you could create a global method

```auto
Function Array2(paramarray a() as variant) as variant()
   Return a
End Function

```

Then you just have to change Array to Array2 in your code.

---

<div class="post-metadata">

**Author:** ![James\_Pitchford](https://forum.xojo.com/user_avatar/forum.xojo.com/james_pitchford/32/4568_2.png) [@James\_Pitchford](https://forum.xojo.com/u/James_Pitchford)\
**Post date:** [September 12, 2021, 2:10pm UTC](https://forum.xojo.com/t/object-array-shenanigans/65497/14 "2021-09-12T14:10:47Z")

</div>

Ah, now that is clever. Thanks Tim.
