Working with variables |
|
Release 9.3
Last modified January 26, 2009 |
![]() ![]() ![]() Print all topics in : "Using ModelBuilder" |
Be sure to read Model elements for a complete definition and discussion of variables.
You can think of a variable as a container that holds a value that can be changed. In the context of a model, a variable can be created and its value used as a tool's parameter value.
There are two basic kinds of variables: data variables and value variables.
Data variables are discussed in Creating model processes, which shows you how to create data variables and connect them to tool parameters.
Value variables are things such as strings, numbers, Booleans (true/false values), spatial references, linear units, extents, and so forth. In short, value variables contain anything but references to data on disk.
There are only minor differences between data and value variables.
You can make a variable from any tool parameter. Right-click the tool and point to Make Variable > From Parameter, then click the parameter, as shown below.
Variables made in this way are automatically connected to the tool.
Another way to create a data or value variable is to right-click the model diagram and click Create Variable. The following dialog box will appear where you choose the data type of the variable you want to create.
Learn more about variable data types.
After choosing a data type for your variable and clicking OK, a new empty variable will be created. You can then connect the variable to one or more tools, set a value for the variable, and/or make it a model parameter.
The Multivalue option will create a variable that allows multiple values to be input, as illustrated below.
Learn more about using the multivalue parameter control.
Learn more about using a multivalue in script tools.
Every parameter and variable has an associated data type. For example, the first parameter for the Dissolve tool is <in_features>, and its data type is feature layer.
Learn more about geoprocessing data types.
You can right-click a variable, click Properties, and change the data type of the variable in the Data Type tab. The list of data types you can choose are those that support the current data type. The example that follows shows how changing a data type can be useful.
The example model below shows the Create Random Points tool with the Constraining Extent input variable as a model parameter. This variable is an Extent data type, which is basically four numbers that define a geographic rectangle. Consider that these four numbers can be retrieved from any feature class, feature dataset, layer, TIN, network, and so on. It can be said that these latter data types (feature class, feature dataset, and so on) support the Extent data type because a geographic extent can be retrieved from them.
To change the Constraining Extent data type, right-click the variable and click properties. In the Data Type tab, select a new data type, as shown below. Only those data types that support the current data type (in this case, Extent) will be shown. For example, the Table data type does not support Extent, so it will not show in the list.
After changing the data type to Feature Class, for example, and opening the model's tool dialog from ArcToolbox, you'd enter a feature class for the Constraining Extent rather than four numbers.
To set the value of a variable, double-click the variable or right-click and click Open. This will open the user interface control and allow you to set the value. You cannot set the value of an in/out derived variable using this method. For in/out derived data variables, you must change the value of the input data variable.
Learn more about in/out derived variables.
NOTE: Once a variable element is connected to a tool parameter, you cannot set the value of the variable using the tool dialog since the parameter control shows the name of the associated variable, not its contents. If you enter a new value using the tool's parameter control, a new variable will be created and connected, and the old variable will be disconnected.
There are two ways you can connect variables to tool parameters, either by using the Connect tool or the tool's dialog.
To use the Connection Tool
By default, when you use the Connect tool to connect a variable to a tool, the variable is assumed to be that of the first parameter whose data type matches the variable. For example, the Add Field tool takes two Boolean variables, Field IsNullable, and Field IsRequired. If you connect a Boolean variable to the Add Field tool, which of these two parameters will it connect to?
To specify which parameter the variable will be connected to, you need to instruct ModelBuilder to display a list of valid parameters when connecting data to tools, as follows:
Variables can be created for environment settings. Right-click the tool and point to Make Variable > From Environment. You can also create a stand-alone variable and associate it with a model's environment setting.
Learn more about model environments.