Variants: Why TypeMismatchException?

I don’t understand why I get a TypeMismatchExcetion in this code:

[code]Dim V As variant
Dim PA() as Picture
V= PA

If V.Type - 4096 = Variant.TypeObject Then
Dim OA() As Object
OA = V ’ Why TypeMismatchException?
End if[/code]

An Array of pictures IS an array of objects so I would think that would work… Is that a bug or just not supposed to be supported?

What I need to find out is what type object array it is

In this situation neither of these work:
V isA Picture ’ gives false
V isA Picture() ’ does not compile

What i wanted to do the the object array (assuming the array was not empty) was

Select case OA(0) Case IsA Class1 ... Case isA Class2 ... Etc

is there another way to approach this?

Thanks,
-Karen

Would introspection work? Perhaps something like this:

[code]Dim V As Variant
Dim PA() As Picture
V = PA

Dim myType As Introspection.TypeInfo = Introspection.GetType(V)

Dim typeName As String = myType.FullName // Picture() in this case[/code]

OA is an array, should be

OA.AddRow V

[quote=464832:@Paul Lefebvre]Would introspection work? Perhaps something like this:

[code]Dim V As Variant
Dim PA() As Picture
V = PA

Dim myType As Introspection.TypeInfo = Introspection.GetType(V)

Dim typeName As String = myType.FullName // Picture() in this case[/code][/quote]

Thanks

That could work for some cases and I may need to use it, but I suspect it would have more overheard and I want this to be fast as it could get called a lot…

I have not used introspection so I may be wrong but it seems to me this would also be a more limited solution because subclasses would have to be explicitly considered when for an IsA they would not need to be… So in general a workaround that can work but less maintainable.

Is the TypeMismatchException a bug, or was this case just not considered when the complier was worked on?

Is it worth a bug report or feature request?

Basically I’m asking is there a reasonable chance at all this might be addressed at some point… if not I won’t bother…

Logically one would expect it to work, but given everything else I am not sure if this relatively obscure case realistically would ever get addressed.

-karen

[quote=464855:@Bernardo Monsalve]OA is an array, should be

OA.AddRow V

Thanks but, I think you may be missing the point of what I am trying to do.

-Karen

If V.Type - 4096 = Variant.TypeObject Then
  Dim OA() As Object 
  OA = V ' Why TypeMismatchException?
End if

the array members are not compatible without casting each one
so you end up having to walk the elements and cast each one to put them in the other array

[quote=464884:@Norman Palardy] If V.Type - 4096 = Variant.TypeObject Then Dim OA() As Object OA = V ' Why TypeMismatchException? End if
the array members are not compatible without casting each one
so you end up having to walk the elements and cast each one to put them in the other array[/quote]

But how do you access them?

V is not an array, but contains an array… Without assigning the contents of V to an array variable how can you get at the elements?

  • karen

one of the shortcomings of Xojo is that in this case you kind of cannot since

  1. you cannot dynamically type a DIM line to be an array of the right type
  2. the variant doesnt tell you what type the array is in some cases

But this isnt new
This has been this way for a decade or more
And one reason I try to never use variants as they cant always be unboxed correctly

[quote=464893:@Norman Palardy]
But this isnt new
This has been this way for a decade or more
And one reason I try to never use variants as they cant always be unboxed correctly[/quote]

I usually try to avoid variants when I can, but I don’t see how to for what I want to do in this case.

As I don’t use them much, this is the first time I have run across this issue… What I tried logically should work and in an ideal world where Xojo was more complete it would… but I suspect this never will as I think they would need a complier person on staff for that to happen

Paul’s suggestion is a workaround I can use in this case, but it’s not an ideal solution. Having an way to get isA to work for array object type in a variant would be.

  • Karen

the other option would be to create your own “variant” like type that you can control & store more info about and use that for these cases instead of trying to use variants which dont seem to help you out as much

and you could add operator_convert methods to it to make your initial code work the way you expect

Following code achieves what you are trying to do:

dim p() as picture
p.append new picture(1,1,32)

dim v as variant = p

dim a as auto = v
dim b() as object = a

select case b(0)
case isa picture
//handle picture
end select
End Sub

thanks… I never adopted the Xojo.framework and since it is now deprecated I never thought to try that!

As Auto can do that, it likely means they can (and should) get variants to be able to do it too, so it’s worth a feedback report.

Thanks,
Karen

Agreed.

It might be worth noting that going through auto can also allow the array to be cast from object() back into picture().

[quote=465076:@Thomas Sanham]Agreed.

It might be worth noting that going through auto can also allow the array to be cast from object() back into picture().[/quote]

I don’t need to do that as this does work:

Dim PArray() as Picture =V

The only issue is that just using variants, you can’t tell what kind of an object an array of objects holds, to be able to know to do that.

-karen

<https://xojo.com/issue/58451>