Variants: Why TypeMismatchException?

  1. 3 weeks ago

    Karen A

    Nov 26 Pre-Release Testers

    I don't understand why I get a TypeMismatchExcetion in this 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

    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

    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
  2. Paul L

    Nov 26 Xojo Inc, Third Party Store

    Would introspection work? Perhaps something like this:

    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
  3. Bernardo M

    is not verified Nov 26 Pre-Release Testers

    @Karen Atkocius OA = V ' Why TypeMismatchException?

    OA is an array, should be

    OA.AddRow V
  4. Karen A

    Nov 26 Pre-Release Testers

    @Paul L Would introspection work? Perhaps something like this:

    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

    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

  5. Karen A

    Nov 26 Pre-Release Testers

    @BernardoMonsalve 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

  6. Norman P

    Nov 26 Pre-Release Testers, Xojo Pro under THE bus
    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

  7. Karen A

    Nov 26 Pre-Release Testers
    Edited 3 weeks ago

    @Norman P 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

    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

  8. Norman P

    Nov 26 Pre-Release Testers, Xojo Pro under THE bus

    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

  9. Karen A

    Nov 26 Pre-Release Testers

    @Norman P 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

    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 <sigh>

    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

  10. Norman P

    Nov 26 Pre-Release Testers, Xojo Pro under THE bus

    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

  11. Thomas S

    is not verified Nov 27 Pre-Release Testers, Xojo Pro Answer

    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
  12. Karen A

    Nov 27 Pre-Release Testers
    Edited 3 weeks ago

    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

  13. Thomas S

    is not verified Nov 27 Pre-Release Testers, Xojo Pro

    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.

    Agreed.

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

  14. Karen A

    Nov 27 Pre-Release Testers

    @Thomas S Agreed.

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

    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

  15. Karen A

    Nov 27 Pre-Release Testers

    Feedback Case #58451

or Sign Up to reply!