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?