KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Setting current position on large arrays
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: May 12, 2011

The current position of an array is stored as an Integer and the Integer data type has a range of values of -32,768 to 32,767. When dealing with arrays, though, the range for the current position of an array is really 1 to 32,767, since array positions must be positive.

This can cause problems when attempting to set the position to a position that is higher than 32,767. Here is an example to illustrate this. Take the following code:

ARRAY TEXT($largeArray_at;33000)
For ($i;1;33000)
    $largeArray_at{$i}:=String($i)
End for

$largeArray_at:=32768
$element:=$largeArray_at{$largeArray_at} `error

When attempting to execute this code, an "Indice out of range" error will occur on the last line. This is because when attempting to assign the position 32,768 to the array, the value is too high and the overflowed value, -32768, is assigned as the position. This value is not a correct array position and thus generates an error.

In order to save the position that is higher than 32,767 a Long Integer can be used to save the position. Using this approach the above code can be made to work correctly by changing it as follows:

ARRAY TEXT($largeArray_at;33000)
C_LONGINT($currentPosition_l)

For ($i;1;33000)
    $largeArray_at{$i}:=String($i)
End for

$currentPosition_l:=32768
$element:=$largeArray_at{$currentPosition_l}

In this version, no error occurs, and the $element variable correctly is assigned the text "32768" as expected.