In all cases except when creating a new Station Global, the new variable will only exist during runtime, so don’t be alarmed if you don’t see the variables during edit time. In the Figure 1 below, the Variables window shows all of the variables that exist at edit time both before and after running all three methods outlined in this document.
Figure 1. Final Results
This article has been divided into three major sections for modern TestStand to explain the various methods with a step-by-step tutorial at the end for users with TestStand 3.5. Modern TestStand users can use the tutorial at the end as well, but this method only outlines how to implement the first method mentioned in the main body and requires more time to implement than the modern method of using expressions to access the TestStand API.
The SetVal<variable type>() method is useful for setting or creating variables that can have values. For example, you can create a numeric using this method, but not a container since a container does not actually have a value. Use something like the following expression to create a variable that has a value.
Runstate.Main.Locals.SetValNumber(“Numeric2”,0x1,5)
In this case, the value is being set for a non-existent variable, so the 0x1 or “PropOption_InsertIfMissing” flag has been added in the property options input. In Figure 2 below, you can see that Locals.Numeric2 exists at runtime after the expression has been evaluated.
Figure 2. Locals.Numeric2 Displayed at Runtime
The major drawback to this method is that you cannot create variables that do not have values. As stated above, this method could not be used to create a container. This brings us to the next method.
The NewSubProperty() method should be used to create Property Objects that cannot have values. The expression below is an example of how to create a variable such as a Container or Object Reference.
Runstate.Main.Locals.NewSubProperty("Container1",PropValType_Container,False,"",0)
Using this method, a variable called “Container1” has been added in Locals. You can see Locals.Container1 has been added at runtime in Figure 3 below.
Figure 3. Container 1 Displayed at Runtime
This isn’t as quick a solution for a variable that can have a value, but it is a great method for creating Containers, Object References, or any other type that does not have a value. You can also use this method or the SetVal method to populate items under programmatically created containers. You will notice, though, that any expressions referencing programmatically created variables, including containers, will not evaluate correctly, so make sure to use the method #NoValidation() around each expression if you have your Sequence Analyzer set to “Analyze File Before Executing”.
One thing to note about this method is that it also allows you to create custom typed variables. To do this, set the PropValType to “PropValType_NamedType” and specify the string of the type to reference in the typeNameParam parameter. For more information on this method, please reference the NewSubProperty Method topic in the NI TestStand Help.
However, you may have an existing type you want to temporarily copy into another location, and going through the effort of creating this variable for only certain situations would require normally unneeded alterations to your sequence file. This leads into the third method.
This method doesn’t necessarily create a new object; it creates a reference to an existing object. This is beneficial when there is an existing object in one location that you want to only temporarily exist in another location. The following expression is an example of what this would look like.
RunState.Main.FileGlobals.SetPropertyObject("Test",0x200 | 0x1,ThisContext.Locals.Test)
This expression will create a reference to Locals.Test in File Globals. Updates to either location will update both variables since one is the actual variable and the other is a reference to that variable. This is mainly because of the flag 0x200 in the middle of the expression, PropOption_NotOwning. The two flags set the new variable as an alias, or reference, to the other and insert the new variable if missing. If you would like more information on Poperty Option flags, please reference the PropertyOptions Constants topic in the NI TestStand help. You can see the “new variable” that exists at runtime in Figure 4 below.
Figure 4. Property Object Displayed at Runtime
There is one final thing to note. If you create a Station Global programmatically, unlike other variables, they are not destroyed after runtime and do not go back to an initial value. Be careful when programmatically creating or editing station globals in case other Sequence Files may be affected by the changes you make.
You can programmatically create variables in the TestStand environment by using the ActiveX/COM Adapter. In order to create them, follow these steps:
For a list of methods to use to create variables, see this help document on the PropertyObject class.