07-18-2012 01:30 PM
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?
07-18-2012 05:26 PM
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,
07-18-2012 05:57 PM
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
07-18-2012 06:12 PM
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).
07-19-2012 10:08 AM
07-19-2012 10:44 AM
Thanks for the explanation -- that helps a lot.