How to know what type of objects an array stored in a variant holds?

Hello. I have a variable of type variant (can’t avoid variants in this case).

This variable may hold anything, including a single-dimensional array (but no multi-dimensional ones). I have to write its content to a BinaryStream, including support for the objects my app is using in this area, possibly as arrays.

My current code works fine as long as the variant doesn’t hold an array of objects.

But, when the variant holds an array of objects (they are always the same type of objects in the whole array), I have to figure out which class of objects it holds so I can save each object accordingly.

I tried several variations, but I either get a TypeMismatch exception or other problems…

What I tried and why it didn’t worked:

• Determining the type of the objects by assigning the first item of the array to a variable of type variant. Then, I could try the “isa” operator against the variable (“isa” won’t work for the whole array). Once I’d know the type, I’d assign the array to a more precise type (e.g. dim MyPictures() As Picture=MyArrayVariant, or iterate with each picture to save it).

But if the array is empty, this raises an OutOfBounds exception, so I’d have to first know whether the array is empty. However, calling UBound on a variant produces a compile-time error (“Parameters are not compatible with this function”/“This is not an array but you are using it as one”). Still more obviously, I can’t use MyVariant.LastIndex either.
For this same reason, I can’t iterate thru the array either.

Assigning the variant to a specific variable of type variant (e.g. dim vl() as variant=value) would allow me to use UBound on the “vl” variable, but then I get a runtime error: TypeMismatch exception at “vl=Value”, because vl is an array of variants and Value holds an array of a specific type (e.g. pictures). A deadlock.

•Built-in functions:

VarType or other generic functions wouldn’t know about my custom classes, so it’s out.
I don’t think introspection is good for that either.
“IsA” doesn’t work for an array, since it would just return the fact it’s an array.

And from there, I’m lost. Any idea?

Please check variant functions in MBS Plugin: Variant Helper functions in MBS Xojo Plugins

Also check variant related feedback cases and put them in your favorites.

1 Like

Thank you. Works great.

Is there a non plugin solution?

make a function, try it and catch exceptions.

On the official forum, where I asked the same question, I got this answer from Kem:

var dArr() as Dictionary
dArr.AddRow new Dictionary

var a as auto = dArr
var oArr() as object = a

With this, you end up having an array of objects in a “valid” variable and you can then iterate thru it.

That only works to determine the array type if the array has non NIL elements … Better than nothing but still significantly lacking for writing general code.

And Auto is deprecated…

Things like this are a significant issue with Xojo … Too many things one reasonably expects should work don’t or have significant unexpected gaps in functionality.

Basically lots of loose ends and details not taken care of the way they should be, even outside of outright bugs.


Completely agreed.