variant.arrayElementType - incorrectly assigns Object to DateTime arrays

When trying to determine the type of array, Xojo incorrectly assigns TypeObject to DateTime arrays, rather than TypeDateTime.
Note also that TypeDateTime is not in the list of VarTypes, but Xojo does autocomplete.

Please confirm this is a bug…

[code]Sub Open() Handles Open
dts.append datetime.now
dts.append datetime.now

Dim v As Variant = dts
Dim i As Integer = v.ArrayElementType

Select Case v.ArrayElementType
Case Variant.TypeDateTime
//correct
Break

Case Variant.TypeObject
//incorrect
Break

End Select

End Sub
[/code]

Theres no VarType for date time which arrayelement type is consistent with

http://documentation.xojo.com/api/data_types/variant.html#variant-arrayelementtype
http://documentation.xojo.com/api/language/vartype.html

in your code i, which holds v.ArrayElementType, is 9 (TypeObject)

[quote=492902:@Norman Palardy]Theres no VarType for date time which arrayelement type is consistent with

[/quote]
There is for Date but not DateTime-seems strange. So its MIA then. Just when you need it! Though the IDE does auto complete for it…which then makes it hard to track down the bugs.

Feedback

  1. dont use variants
  2. when you think you need a variant - think again and maybe make your own type
  3. and if you still think you need a variant see rule #1

kidding aside there are enough edge cases with variants that they can become quite painful
(see https://forum.xojo.com/60787-unchanged-files-having-changes-continues-to-boil-my-backside)

the MIA is the VarType function - not the Variant.DateTimeAccessor :frowning:

Yes, I try to avoid them, though in this case I am rebuilding class properties from JSON code, and don’t know what the result is until I’ve read it.

might need to serialize both the type and value

in xml you might use an attribute on the node like

<property type="integer">123</property>

you could do something like that in json as well but that turns each property value into … maybe an array that holds 1 node that is the type & 1 that is the value

{
  "name": [
    {
      "type": "integer"
    },
    {
      "value": <whatever the right representation for the value type for json is>
    }
  ]
}

TypeDateTime exists and is 38, not 9, so seems some bug. Both Date and DateTime are objects, but Xojo differentiates both in variants (7 and 38).

You need a complete sample proofing the bad behavior.

Yes, that might improve things a bit, thanks. Although then I become reliant on comparing manually entered text fields rather than enumerations…

[quote=492911:@Rick Araujo]TypeDateTime exists and is 38, not 9, so seems some bug. Both Date and DateTime are objects, but Xojo differentiates both in variants (7 and 38).

You need a complete sample proofing the bad behavior.[/quote]
The example is posted above
ArrayElementType does not return DateTime it returns “Object” as is consistent with VarType (which is would seem it uses internally)

Does not run, does not proof.

Incomplete. What’s dts? dts() As Object, dts() As Variant, what? Incomplete.

  dim dts() as DateTime

  dts.append datetime.now
  dts.append datetime.now
  
  Dim v As Variant = dts
  Dim i As Integer = v.ArrayElementType
  
  Select Case v.ArrayElementType
  Case Variant.TypeDateTime
    //correct
    Break
    
  Case Variant.TypeObject
    //incorrect
    Break  
    
  End Select

typo fixed

Is this syntax possible since what version? “Array” as data type? Able to receive an object?

typo fixed

ArrayElementType() has a bug. It occurs with arrays of Date or DateTime.

:slight_smile:

Vartype(dts) = 4096+9
Vartype(dts(0)) = 38

As Xojo chose that some objects had an alternative “type” meaning, the compiler should have a way to infer a 4096+38 VarType for an array of DateTime objects. So, 9, 7 and 38 are objects, but the 7 and 38 should fake they are not, as they had a proper type.

[quote=492947:@Norman Palardy]The example is posted above
ArrayElementType does not return DateTime it returns “Object” as is consistent with VarType (which is would seem it uses internally)[/quote]
But not consistent with the manual notes for dateTIme which quote: [quote]Although DateTime is a class that is subclassed from Object, VarType identifies a DateTime as a DateTime data type (Type=38) rather than an Object (Type=9). [/quote]

1 Like