How can convert the empty array to a real array?

sub mytest
    GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
    Dim dataRow() as integer
    dataRow = array()
    ReDim dataRow(0 to 1) as integer
    dataRow(0) = 1
    print dataRow(0)
end sub

It print a empty window,no “1” in the window,
First dataRow is an empty array,i redim it with “ReDim dataRow(0 to 1) as integer”, why can’t print its element after assign value ?

(First, some nitpicks. The GlobalScope.BasicLibraries.loadLibrary("ScriptForge") is completely unnecessary in your example, and only distracts. Also, if you want a single-element array, you should use (0 to 0) - otherwise, what your unused element is intended for in the demo?)

This is a complex bug.

  1. If you put breakpoints, and inspect dataRow at each line (I know that everyone doing any programming uses debugger and watch window, right?), you will see that dataRow is Integer(0 to -1) after its Dim statement; it is Variant(0 to -1) after assignment of array(); and it is Empty(0 to 1) after ReDim. The final type of elements is, as told, Empty - and that is the type that can only one possible value: Empty. You can assign any value to it; and the result is still Empty. This is what happens in your case.
  2. If you drop the assignment of array(), it starts working. The difference is that it doesn’t have to change the type of elements, so it is Integer(0 to 1) after ReDim, meaning that elements store assigned numbers as expected.
  3. Now, with removed assignment of array(), try to change the element type - either in Dim, or in ReDim. And this time, the code will fail compilation, with message “BASIC syntax error. Variable dataRow already defined.”. This shows, that you can not re-define the type of elements.

(Some?) problems are:

  • Why is there the syntax to specify the type in ReDim, when you can’t actually change it?
  • Why isn’t this documented?
  • Why is it still allowed to re-define it indirectly - using assignment of another array (e.g., created by Array function)?
  • Why is it allowed to ReDim such an indirectly defined array with another element type, and why does it not fail, but silently assigns a different type?

While this is a topic for a bug report, the expected use is that you don’t use ReDims that change the element type of arrays.

1 Like

The Redim operator is very often used in LO Basic programs, so the possibilities to change its behavior are limited.
I think that specifying the type of a variable in Redim is only allowed when the variable is first defined (in the absence of Option Explicit). In other cases, the type specification construct should, alas, be silently ignored.

As far as I understand, Empty is a subtype of Variant.

Every type is a subtype of Variant, in the sense that values of that type can be stored in a variable of type Variant. Empty is not an exception; however, type Empty itself is a fixed type.

We can write

Dim var as Long

but we can’t

Dim var as Empty

@sokol92 that’s not because Empty is special, but because it’s not exposed.

1 Like

IMHO, Empty is not a data type. It is the default value of a variant.

@Villeroy it is both a data type, and that type’s only value.

@mikekaganski , thanks for the information, so in this regard there is a difference from VBA.

more confusing for the reader, and the interpreter :expressionless:
it works without.

or rather use :

  dataRow = array(1234) 
  print dataRow(0) 

image