KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: "Indice out of range" error
PRODUCT: 4D | VERSION: 6.5 | PLATFORM: Mac & Win
Published On: August 17, 2001
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

When you are using arrays and you attempt to assign a value to one element of an array that has not yet been created, you get the painfully common "Indice out of range" error.

A good way to fight that error is to retrieve the size of the array before the assignment and create on the fly the element needed. For example, the following code assigns a serie of values through a loop.

For ($i;1;25)
 $MyArray{$i}:="Value "+String($i)
End for


If the array does not have the 25 first elements already created, you will run into the "Indice out of range error."

The same code when modified as follows will create the elements when needed and will not encounter that error.

$PreviousSize:=Size of array($MyArray)
For ($i;1;25)
If($i>$PreviousSize)
 INSERT ELEMENT($MyArray;$i)
End if
$MyArray{$i}:="Value "+String($i)
End for