Programmatically Creating Variables in TestStand


There are multiple methods to create new variables in TestStand. This article will outline three main cases.


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.

SetVal<variable type>()

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.


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.


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.

TestStand 3.5 Method

You can programmatically create variables in the TestStand environment by using the ActiveX/COM Adapter. In order to create them, follow these steps:

  1. Choose the ActiveX/COM Adapter from the adapter list
  2. Insert an Action step
  3. Right Click the action step and select Specify Module
  4. Set the Automation Server to NI TestStand 3.5 API (or your current version of the TestStand API)
  5. In the Edit ActiveX/COM call dialog box, set Object Reference to ThisContext
  6. Set the Object Class to PropertyObject
  7. Set Action to Call Method
  8. Set Method to SetValXXX where XXX is the type of variable you want to create.
  9. In the Parameters list set the lookupString value to StationGlobals.ZZZ where ZZZ is the name of the new variable.
    You need to put quotes around the lookupString Value.
  10. Set the options value to 1. This will force the variable to be created if it is not there. If you set the options value to 5, the variable
    will be created if it does not exist; however, nothing will happen to it if it already exists.
  11. Set the newValue value to whatever you want the value of the variable to be.

For a list of methods to use to create variables, see this help document on the PropertyObject class.

Was this information helpful?