NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

remove array element from dynamically created array

I'm having an awful time trying to delete an array element.  Sounds like something pretty simple, and I'm sure I've made some stupid mistake here, but I just can't find it -- so I'm asking for some help:

 

Attached file.

 

It's got two arrays of custom typedef containers in FileGlobals.  One is already filled out with values (notice the array elements are named), and the other is an empty array.

Set a breakpoint at the beginning of your code, and run MainSequence.

Step through, and have a watch expression on FileGlobals.* arrays.

 

The code goes through and adds a few elements to the "DynamicArray", and names each element.

 

Then it goes and tries to delete the elements from the "Static" array using the names.

This seems to work just fine.

 

Then it goes and tries to delete the elements from the "Dynamic" array and gets no errors, but does not delete the elements.  Why not? I would expect it to delete the elements the same way as it did for the static array. 

 

What am I doing wrong?

0 Kudos
Message 1 of 6
(6,252 Views)

Your problem isn't with the delete.  It has to do with how you are inserting elements into the array.  the += operator for some reason doesn't seem to be doing it correctly.

 

Instead you should use:

FileGlobals.DynamicArray.InsertSubProperty("[" + Str(GetNumElements(FileGlobals.DynamicArray)) + "]", 1, GetNumElements(FileGlobals.DynamicArray), Locals.DemoStructure.Clone("",0))

 

I'm assuming you can't use the built in Array functions for TestStand otherwise you would be using: InsertElements and RemoveElements????

 

Anyhow, the problem is with your insertion.

 

Regards,

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
Message 2 of 6
(6,241 Views)

This looks like a bug with the += operator when you do the following:

 

FileGlobals.DynamicArray += {Locals.DemoStructure},

You can workaround this problem by doing this instead:

 

SetNumElements(FileGlobals.DynamicArray, GetNumElements(FileGlobals.DynamicArray) + 1),
FileGlobals.DynamicArray[GetNumElements(FileGlobals.DynamicArray) - 1] = Locals.DemoStructure,

I've submitted this issue to our tracking database for further consideration.

Hope this helps,

-Doug

0 Kudos
Message 3 of 6
(6,238 Views)

Thanks!!!

 

Changing how I added the elements to the array fixed it. 

 

I'm not using RemoveElements() because it only works on numeric indexes of the array, and I want to delete an array element by name.  (I know there's a uniqueness problem here, but I'm OK with that).  If I go through and find the numeric index of the named element I want I can manage to delete it with RemoveElements, but that ended up longer and more complicated than I wanted to deal with -- which lead to what I tried doing.

 

So NI -- what's the guidelines for using Array += {value} notation -- I've got that all over the place in my code, and want some suggestions as to when it is appropriate or not appropriate to use (specificially based on the problem above).

0 Kudos
Message 4 of 6
(6,236 Views)
It should have been ok to use it like you are doing, however the way it is appending element for arrays of objects is resulting in those elements not getting their parent properly set as being the array. This will likely be fixed in a future version of Teststand. One side effect of the parent not getting set is that the delete fails because it tries to get the parent of the element to remove it from the array, but can't. Another potential issue is that if you used a non-temporary array on the right side it would end up sharing the elements with both arrays which is not supposed to happen and is not supported (it should probably be making copies of the elements instead). Arrays of basic types do not have these issues.

So if you are just using += with arrays of basic types or if you are using only temporary arrays on the right hand side and don't run into any issues due to the elements not having a pointer to their parent array then you should be ok.
0 Kudos
Message 5 of 6
(6,223 Views)

Thanks for the explanation -- that helps a lot.

 

0 Kudos
Message 6 of 6
(6,220 Views)