NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Adding Elements To A Container Thats An Element In An Array

I'm trying to programmatically (using expressions) create an Array of Containers and set the container elements using an expression:

ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,False,"",0)),
ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO.IOToBeProcessed",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,False,"",0)),
Locals.SetValString("IO.IOToBeProcessed.Name",PropOption_InsertIfMissing,""),
Locals.SetValNumber("IO.IOToBeProcessed.Modbus Address",PropOption_InsertIfMissing,0),
Locals.SetValString("IO.IOToBeProcessed.Type",PropOption_InsertIfMissing,""),
Locals.SetValString("IO.IOToBeProcessed.Raw Signal",PropOption_InsertIfMissing,""),
Locals.SetValNumber("IO.IOToBeProcessed.Raw Min",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Raw Max",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Scale Min",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Scale Max",PropOption_InsertIfMissing,0),
Locals.SetValString("IO.IOToBeProcessed.Tag",PropOption_InsertIfMissing,""),
Locals.SetValString("IO.IOToBeProcessed.Notes",PropOption_InsertIfMissing,"")

The above works fine, but it does not create IOToBeProcessed as an array of the elements I added after creating that container. If I change my code to this:

ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,False,"",0)),
ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO.IOToBeProcessed",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,True,"",0)),
Locals.SetValString("IO.IOToBeProcessed.Name",PropOption_InsertIfMissing,""),
Locals.SetValNumber("IO.IOToBeProcessed.Modbus Address",PropOption_InsertIfMissing,0),
Locals.SetValString("IO.IOToBeProcessed.Type",PropOption_InsertIfMissing,""),
Locals.SetValString("IO.IOToBeProcessed.Raw Signal",PropOption_InsertIfMissing,""),
Locals.SetValNumber("IO.IOToBeProcessed.Raw Min",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Raw Max",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Scale Min",PropOption_InsertIfMissing,0),
Locals.SetValNumber("IO.IOToBeProcessed.Scale Max",PropOption_InsertIfMissing,0),
Locals.SetValString("IO.IOToBeProcessed.Tag",PropOption_InsertIfMissing,""),
Locals.SetValString("IO.IOToBeProcessed.Notes",PropOption_InsertIfMissing,"")

This sets the property "AsArray" for the container, so IOToBeProcessed would then be created as an array, which is what I need. Unfortunately, TestStand then starts complaining that I can't add elements to the container as its part of an array:

The post-expression for the step 'IO' could not be evaluated.
Error in call to TestStand API member 'PropertyObject.SetValString'.
You cannot create a subproperty in an item that is not a container.
Error accessing item 'IO.IOToBeProcessed.Name'.

Where am I going wrong, how do I get IOToBeProcessed to be an array of said elements?

 

Thanks in advance

0 Kudos
Message 1 of 2
(2,957 Views)

 

Hey Liam,

 

An array can only be one type of element, and you are attempting to create an array that contains multiple different data types. The following two lines create IO (container) which contains IOToBeProcessed (array of containers) -

 

 

ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,False,"",0)),
ThisContext.AsPropertyObject.SetPropertyObject("Locals.IO.IOToBeProcessed",PropOption_InsertIfMissing,ThisContext.Engine.NewPropertyObject(PropValType_Container,True,"",0)),

 

Therefore, to access or create new properties within the array of containers, you will have to index the array and create an element within each specific container in the array. For example, in the following three lines, within the first container in the array (element 0), Name and Modbus Address properties are being added to that container, and to the second container in the array (element 1), Type is being added. 

 

Locals.SetValString("IO.IOToBeProcessed[0].Name",PropOption_InsertIfMissing,""),
Locals.SetValNumber("IO.IOToBeProcessed[0].Modbus Address",PropOption_InsertIfMissing,0),
Locals.SetValString("IO.IOToBeProcessed[1].Type",PropOption_InsertIfMissing,""),

 See the variables window during runtime:

image.png

 

Message 2 of 2
(2,923 Views)