Ask a Question

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

SOLVED
Pakema
Contributor

[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?

 

4 REPLIES 4
Marsha_R
Community Hero

What error?  The more information you give us in the beginning, the easier it is to find an answer for you.  


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame

error in log "Script execution was interrupted"

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

Thanks a lot! It worked!

cancel
Showing results for 
Search instead for 
Did you mean: