NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Strange behaviour in For Each execution

Solved!
Go to solution

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.

Rolf Kalbermatter
Averna BV
0 Kudos
Message 1 of 8
(2,710 Views)
Solution
Accepted by topic author rolfk

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

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 2 of 8
(2,705 Views)
Solution
Accepted by topic author rolfk

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:

  • Array to Iterate Over—The expression that determines the array over which the loop iterates. The steps within the block execute once for each element in the array.
  • Array Element Loop Variables (optional)—Expressions that determine the location into which to store information about the current loop iteration.
    • Current Element—An expression that determines the variable or property into which to store the current element in the Array to Iterate Over option during each iteration of the loop. The location the expression specifies must be of the same type as the element type for the Array to Iterate Over option. Typically, you specify a local variable so the steps in the block can read or modify the current array element by referring to the local variable.
    • Current Offset—An expression that determines the numeric variable or property into which to store current offset in the Array to Iterate Over option during each iteration of the loop. For zero-based one-dimensional arrays, the offset is equal to the index of the array. For multi-dimensional arrays, the offset is the linearized index of the array.
    • Current Subscript—An expression that determines the string variable or property into which to store the subscript of the current element in the Array to Iterate Over option. Examples of the subscripts include "[23]" and "[1][3]".

 

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,

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

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.

Rolf Kalbermatter
Averna BV
Message 4 of 8
(2,692 Views)

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,

 

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 5 of 8
(2,679 Views)

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.

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

@~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! Smiley Surprised

Rolf Kalbermatter
Averna BV
0 Kudos
Message 7 of 8
(2,671 Views)

Gotcha!!

 

That's no fun when we get the head scratchers.  Glad we've figured it out.

 

Have a good one,

jigg
CTA, CLA
testeract.com
~Will work for kudos and/or BBQ~
0 Kudos
Message 8 of 8
(2,669 Views)