Forum Discussion

Pakema's avatar
Pakema
Contributor
10 years ago

[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 threeDimArray(4, 4, 9)

 

Script 

 

Sub Ex1

Dim LocalVar_TmpArr(1)

Erase LocalVar_TmpArr

ReDim LocalVar_ObjArr(2) '<-- Script execution was interrupted

End Sub ' Ex1

 

 Why error?

 

  • 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
  • sbkeenan's avatar
    sbkeenan
    Frequent Contributor

    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
  • What error?  The more information you give us in the beginning, the easier it is to find an answer for you.  

    • Pakema's avatar
      Pakema
      Contributor

      error in log "Script execution was interrupted"