paul_igoe
13 years agoContributor
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 :-)
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 :-)