02-16-2016 08:43 AM
I had this code where I had some array of values in
Locals.Conditions.Channels[]
and a local variable
Local.Channel
I then used these two with a For Each statement with the array as Array object to iterate over and the skalar local varaible as Current Element of the statement.
Inside that loop I did reuse Local.Channel as local variable depending on some other conditions and assigning new values to it.
To my huge surprise the array was modified as I assigned new values to Local.Channel
This is certainly not intuitive and I couldn't find any documentation that said this would have to be expected. That said I have trouble to find a lot of things in the online manual so I might have missed something here.
This is with TestStand 2014 SP1 on Windows 7 Professional.
Solved! Go to Solution.
02-16-2016 09:00 AM
Rolf,
i do think that this is intended behavior. Reading the help for the "For Each" step states:
Current Element—An expression that determines the variable or property in which to reference the current element in the array during each iteration of the loop.
Please notice that the help clearly states "reference", not "copy". The following text, while not being too clear on explaining this, indicates that the "Current Element" expression enables the developer to have an simpler access to the array element rather then building a custom loop iterator (as For Each doesn't expose one!) and always state Array[CustomIterator] in order to access the individual array elements.
Norbert
02-16-2016 09:11 AM
ForEach Loop Edit Tab
Use the ForEach Loop edit tab in the TestStand Sequence Editor to configure the For Each step.
The ForEach Loop edit tab contains the following options:
Read the Red Bold part. I know it isn't as clear as saying: "The current element is a reference to the array element" but it is kinda hidden in there.
Also, I cannot think of a case where you would need to change the current element without modifying the array element. I'm pretty sure you've figured out by now to use a different variable to track whatever it is you need.
If you are unsatisfied with the manual you can always file a CAR with NI.
Hope this helps,
02-16-2016 09:26 AM - edited 02-16-2016 09:36 AM
Thanks guys. I agree that it can be deduced when reading the quoted parts carefully, but it wasn't really clear that the Local Variable would receive a reference to the original array element. It's also not really intuitive coming from other programming languages like C(++) where references have to be explicitedly declared and used using the * and & operand.
And yes of course creating a separate local variable was the solution, once I figured out what was happening but my initial surprise when the application was misbehaving was pretty big.
Also note the difference in the two quotes: Norbert's quote contains "reference", while jigg's quote uses "store". So someone probably played with that help text already in an attempt to make this more clear for TS2014.
02-16-2016 09:58 AM
ForEach does not exist in C++ except in the newer C++11. So maybe that's why it isn't familiar?
In C# if you say foreach(String str in Arr) then str is a reference to the element in the array and will exhibit the same behavior as you've seen in TestStand. Since TestStand is built on C# it makes sense to me why it would behave more like C#.
To say: "was misbehaving was pretty big." seems kind of harsh. I think you meant to say: "behaving in a manner I didn't expect." Because I'm sure it is behaving exactly as the folks at NI intended it to. 😉
Cheers,
02-16-2016 09:59 AM
BTW- I grabbed mine out of TestStand 2010. Hence the verbage difference. I should have looked in 2014. I'm working out of both versions and didn't realize I was in 2010 at the time.
02-16-2016 10:25 AM
@~jiggawax~ wrote:
To say: "was misbehaving was pretty big." seems kind of harsh. I think you meant to say: "behaving in a manner I didn't expect." Because I'm sure it is behaving exactly as the folks at NI intended it to. 😉
Cheers,
I meant my test application, not TestStand in itself. It did run ok in the first loop iteration (there were more outer loops around this code to cycle power supply voltages and temperatures) and then went completely havoc measuring the wrong channels!
02-16-2016 10:28 AM
Gotcha!!
That's no fun when we get the head scratchers. Glad we've figured it out.
Have a good one,