Forum Discussion

paul_igoe's avatar
paul_igoe
Contributor
13 years ago

ArrayType DeleteItem in loop

Hi,



I had a problem with the Array Type DeleteItem method, not sure if I am not using it properly or if the TC documentation is incorrect?



Basically I had something like the JScript example at http://support.smartbear.com/viewarticle/31611/ :



function DeleteItem()

{

    // Specifies the property of the array type

    // to obtain information about

    var Prop = ODT.Classes.TestClass.MyProperty;

    // Iterates through the array items

    for (var i = 0; i < Prop.Count; i++)

       // Checks whether the current item's value is empty

       if ( Prop.Items(i).Value == aqObject.EmptyVariant )

         // Deletes the item

         Prop.DeleteItem(i);

}



I was getting index out of bounds exceptions, and after a bit of debugging seems with each delete the array shrinks, so the indices are out.



Instead, I think you need to do something like the following, to iterate through the array from the end, instead of the beginning!



function DeleteItem()

{

    // Specifies the property of the array type

    // to obtain information about

    var Prop = ODT.Classes.TestClass.MyProperty;

    // Iterates through the array items

    for (var i = Prop.Count-1; i>=0; i--)

       // Checks whether the current item's value is empty

       if ( Prop.Items(i).Value == aqObject.EmptyVariant )

         // Deletes the item

         Prop.DeleteItem(i);

}



At least, works for me :-)

1 Reply


  • Hi Paul,


     


    That's very strange as, actually, your script iterates through the same elements. The only difference is the order of accessing the elements.


     


    I've executed the script from the help documentation in TestComplete 9.31 - it works fine on my computer.