Expressions
An expression, in programming, is a combination of symbols that represents a value. The expression is interpreted according to the rules of the programming language and a value is returned. The Expression Builder provides a quick and convenient way to create and evaluate BASIC expressions from directly within the parameters of a task step. Expressions work by taking the text found between percentage (%) signs and passing it to the BASIC expression interpreter. During runtime, the BASIC interpreter replaces the original expression (including the percentage signs) and returns the expression's resulting value instead.
Using Expressions
Many actions that normally require expressions may already be built into activities. However, you can use expressions to further expand an action's capabilities. For instance, you can build expressions that resolve complex, dynamic data at runtime with the use of variables, functions, extended functions and operators.
Example 1 - Simple mathematical expression
In the following example, a task runs a single step that performs a simple mathematical calculation and displays the results in a message box. To create this task:
-
In the Task Builder, add a Message Dialog activity to the Steps panel.
-
From the General properties of this activity, enter %7+7% in the Message to display parameter (as shown below) and click OK to save changes.
-
From the Task Builder's ribbon, click the Run button to start the task.
Since the phrase 7+7 is surrounded in percentage signs, knows that it should
try to resolve the expression and not display it literally. To resolve
the expression, at runtime, the value between the percentage signs is
passed to the BASIC expression interpreter, where it is processed and
the result is returned. A message dialog should appear with the value
of 14 (as shown below).
Example 2 - Expression with variable
Expressions can also contain variables. Taking the previous example a step further, assume we now want the task to give the user the ability to enter a number he/she would like squared.
-
Use the Create variable activity to create a variable named NUMTOSQUARE with the initial value of 1. This variable will be used during runtime to retain the user's response to the question.
-
Use the Input dialog activity as the second step to display the question What number would you like squared? and populate the NUMTOSQUAREvariable with the answer.
-
Use a Message Dialog as the last step which will display the result by use of an expression multiplying the variable by itself. %NUMTOSQUARE *.
The complete task is displayed below in AML format. For convenience, the AML code can simply be copied and pasted directly into the Steps panel of the Task Builder.
<!-- Create the variable. --> <AMVARIABLE NAME= <!-- Ask the user to enter a number. --> <AMINPUTBOX <!-- Perform the calculation using an inline expression. --> <AMMESSAGEBOX%>
Notes
Notice that we first had to create the variable we intend to use. All variables to be passed into a scripting language, either explicitly through the use of a BASIC script step or implicitly through the use an expression, need to be created first. You can accomplish this using the Create Variable action.
Variable names are not case sensitive; the variables are capitalized here for clarity.
Because each expression is passed to the scripting language engine, any command available from a common BASIC script is available to you for use in expressions in your step parameters. Refer to BASIC Scripting for details on these powerful commands.