A very basic question

When a variant variable contains an array i.e. myvariant.IsArray returns True how to get the array Ubound?

Best regards.

First copy it back to an array of the correct type.

Dim myArray() As Integer myArray = myVariant Dim ub As Integer = myArray.Ubound

Thank you Paul but…

If the variant type it is a class, how can I declare an array of “not known at compile time” items?

This should work:

dim myArray() as Object = myVariant
ub = myArray.Ubound

If you have subclass based on class, you can assign it to a variable declared as either. “Object” is the base class of all of your classes.

However, if you array is of, for example, integer, this won’t work.

[quote=61011:@Maurizio Rossi]Thank you Paul but…

If the variant type it is a class, how can I declare an array of “not known at compile time” items?[/quote]

Dim NotKnownAtCompileTimeObjects() As Object
select case DesiredType
case "CLASS1"
CLASS1(NotKnownAtCompileTimeObjects(0)).Left = 12
case "CLASS2"
CLASS1(NotKnownAtCompileTimeObjects(0)).Left = 12
end select

I am not sure if this is what you are looking for but you must use casting to use specific properties, methods, or etc. The way casting works is, for example this:

Dim obj As Class1
obj = New SubclassOfClass1
SubclassOfClass1(obj).SpecificProperty = 12 //specific property to this subclass

Or you could use an interface with something like this:

Dim obj As New Class1 //class1 uses the classInterface interface
classInterface(obj).SpecificProperty = 12 //specific property to this interface (can be casted for any object that uses the classInterface, interface)

Hope this helps

Sorry Kem,

dim myArray() as Object = myVariant
ub = myArray.Ubound

fails with TypeMismatchException.

Ok, found it.

dim v2() As Variant = Array(myvariant)
theubound = v2.Ubound

Thank you all.

Well, that’s what I get for not trying the code myself first. Yet, it should work because the assignment of a class array to an object array works.

Your solution won’t work either. The Ubound returned will always be 0.

I also tried doing this through an Interface, but that doesn’t work either. It looks like Xojo is only happy if you assign the Variant back to a variable declared as exactly the same type.

For sure this problem must have a solution:

during debug Xojo IDE (written in Xojo) it is able to display the number of elements in a variant containing an array of objects…

Hey, you’re right, AND it know what the class of the array is.

[quote=61011:@Maurizio Rossi]
If the variant type it is a class, how can I declare an array of “not known at compile time” items?[/quote]

I know I’ll get a LOT of flak from folks about saying this about variants BUT I’d give you the same advice we gave on <https://xojo.com/issue/31799>
Variants have some uses but mostly inflict pain & suffering on anyone relying on them - eventually

A set of container classes or list classes for the data you expect to deal with is probably a good design choice so the compiler can help you out as much as possible.
Possibly an interface since “variant” lets you treat “all thos data the same” - so an interface is probably a good plan.

Otherwise you’ll run into the case in 31799 where you can put data in and can’t get it out again

Quite literally we’ve weeded out a LOT of issues in the IDE by deciding to NOT use variants and getting rid of their use where we can

A LOT of the flip flopping of values in VCP projects came from VARIANTS (and if you use version control you know what I mean about the flip flop)

Thank you Norman,

I’ll try to follow your suggestions and avoid variant as much as possible.
At this moment my question it is only a curiosity related to the language features.

Best regards.

[quote=61032:@Maurizio Rossi]For sure this problem must have a solution:

during debug Xojo IDE (written in Xojo) it is able to display the number of elements in a variant containing an array of objects…[/quote]
The debugger has additional information that normal runtime does not. And it probably uses introspection as well.

I have tried introspection but no useful information for this.