How to clear array contents in vbscript?

I declared a two-dimensional array in the function library and linked it to the test. In action1 of the test, I tried to clear the array using the delete operator.
My code is
In the function library

Dim strVerifyAry(25,6) 

In action1,

 erase strVerifyAry 

Error message

 Run Error - Type mismatch: 'Erase' 

How to clear the contents of this array?

+4
source share
4 answers

It works for me in simple VBScript, so this is most likely a problem with any QTP engine used to run VBScript code. You should emulate the behavior of Erase for a 2-dimensional array as follows:

 Sub EraseArray(ByRef arr) For i = 0 To UBound(arr, 1) For j = 0 To UBound(arr, 2) If IsObject(arr(i, j)) Then Set arr(i, j) = Nothing Else arr(i, j) = Empty End If Next Next End Sub 

Or, for example, if you do not want to set fields containing objects to Nothing :

 Sub EraseArray(ByRef arr) For i = 0 To UBound(arr, 1) For j = 0 To UBound(arr, 2) arr(i, j) = Empty Next Next End Sub 
+3
source

I don’t quite understand why, but you can create sub as

 Public Sub DoErase (byRef Ary) Erase Ary End Sub 

in the library and call it from the action as follows:

 DoErase StrVerifyAry 

and it works.

Update: No, it is not. The array is successfully passed to DoErase , and the DoErase call works fine, but after that the test can still refer to the elements of the array that Erase should have erased.

If the test declares an array, it works fine ( Erase erases the elements).

This is very strange and probably related to bizarre areas in function libraries.

Please let us know if you ever find out what is going on here ...

+1
source

This made me dunk all day, so I wanted to post an answer for future reference. I populated the array with the Split command, and then he needed to delete it before the script started the process again. Nothing I tried will remove or clear the array, and the next use of Split is just added to the previous elements of the array.
Having tried the loop "array = Nothing" above, I finally managed to create the error "This array is fixed or locked", which I examined. It turns out that I used an array in the "For Everyone ..." loop, which blocks the array, so it cannot be erased or cleared. Additional information is available HERE:

0
source

This is a cop, I know ... Do not vote me!

In some cases, you can use a dictionary collection rather than an array. Then use RemoveAll when you want to clean it. This does not help when your array was created using the split function or something else, but it can help in other use cases.

 Set myDict = CreateObject("Scripting.Dictionary") ... myDict.RemoveAll 

Refer to: https://www.w3schools.com/asp/asp_ref_dictionary.asp

0
source

Source: https://habr.com/ru/post/1484601/


All Articles