Forum Discussion

Pakema's avatar
Pakema
Contributor
9 years ago
Solved

[TC 10.6|VBS] Script execution was interrupted after ReDim array

Hi There   In MSDN (VBS) in example https://msdn.microsoft.com/ru-ru/library/83zyeke9.aspx    Dim threeDimArray(9, 9, 9), twoDimArray(9, 9) As Integer Erase threeDimArray, twoDimArray ReDim th...
  • sbkeenan's avatar
    9 years ago

    Hi Pakema

     

    Your first example is using Visual Basic, not VBScript.  Also, in your script example, you are using Redim on a different array (ObjArr) - your initial array is referenced by TmpArr.

     

    Anyway, what happens in VBScript is this, when you declare a fixed array, i.e. you specify its size, e.g. Dim myArray(2), you can use the erase statement to clear its content.  You can still reference it's indexes, but they will be empty.

     

    However, when you declare a dynamic array, e.g. Dim myArray(), you must use the Redim statement to specify its size before you can use it, e.g. Redim myArray(5).  This will allow you to fill it with data.  Using Redim on the same array will allow you to re-size the array, but all data will be lost. (You can use Redim Preserve to re-size the array, keeping its data, although if you make it smaller, then some data will obviously be lost.  Also, if you have previously used the Erase statement, then all data is lost anyway, so Redim Preserve serves no function).

     

    When you use the Erase statement on a dynamic array, it's not just the data that is lost, it loses its size too, i.e. the variable is still available, but cannot be used until you specify a new size.

     

    I have provided a couple of examples below, which I hope, explains the points made above.

     

    sub testDynamicArray
    	dim myArray()	'this makes it a dynamic array
    	redim myArray(4)
    	myArray(0) = 0
    	myArray(1) = 1
    	myArray(2) = 2
    	myArray(3) = 3
    
    	log.message("The value at index 0 is " & myArray(0))
    	log.message("The value at index 1 is " & myArray(1))
    	log.message("The value at index 2 is " & myArray(2))
    	log.message("The value at index 3 is " & myArray(3))
    	
    	redim preserve myArray(5)
    	
    	log.message("The value at index 0 is " & myArray(0))
    	log.message("The value at index 1 is " & myArray(1))
    	log.message("The value at index 2 is " & myArray(2))
    	log.message("The value at index 3 is " & myArray(3))
    	log.message("The value at index 4 is " & myArray(4))
    	
    	erase myArray
    	redim myArray(5)	'we need to specify the size again
    	
    	myArray(4) = 5
    	log.message("The value at index 0 is " & myArray(0))
    	log.message("The value at index 1 is " & myArray(1))
    	log.message("The value at index 2 is " & myArray(2))
    	log.message("The value at index 3 is " & myArray(3))
    	log.message("The value at index 4 is " & myArray(4))
    end sub
    
    
    sub testFixedArray
    	dim myArray(4)	'this makes it a fixed array
    	myArray(0) = 0
    	myArray(1) = 1
    	myArray(2) = 2
    	myArray(3) = 3
    
    	log.message("The value at index 0 is " & myArray(0))
    	log.message("The value at index 1 is " & myArray(1))
    	log.message("The value at index 2 is " & myArray(2))
    	log.message("The value at index 3 is " & myArray(3))
    
    	erase myArray
    	
    	log.message("The value at index 0 is " & myArray(0))
    	log.message("The value at index 1 is " & myArray(1))
    	log.message("The value at index 2 is " & myArray(2))
    	log.message("The value at index 3 is " & myArray(3))
    end sub