Using Expressions
Previous Top Next

Expressions are embedded functions, variables, equations and dataset fields inside a string that are parsed at runtime to return dynamic information inside the string.

For example, the following string contains an embedded expression:

Today's date is <%=Now()%>

When this string is parsed, this is what it produces:

Today’s date is 2008/05/15 08:53:13 AM

Expressions can be inserted into any text field of the component configurations and can be done by pressing on the Insert Expression button.

graphic

graphic

The Expression dialog helps you to construct your own expressions by selecting a field from a dataset, a variable or a function and testing the result before the expression is used in your action.

Take a look at the following component configuration:

graphic

Note that the path field contains “<%=ORL.WindowsDirectory%>\myfile.txt”. The expression between the <% %> tags will return the path to the windows directory, when this expression is parsed at runtime the following will be produced: “C:\Windows\myfile.txt”.

In the contents the expression <%=Now()%> will be replaced with the current date and time when the task executes.

As you can see expressions adds a lot of flexibility to actions in a task in the fact that dynamic information can be used when the actions execute.

If you worked with ASP before you will see that the expression engine in Optitask works on the same principle.

There are primarily two type of expressions that you can use. The first most common expression will simply return a value from a function, an equation, a variable or a dataset. Expressions that return a value must have an equal sign right next to the open expression tag, for example: <%=Age%>
Without the equal sign the parser will not return a value and will assume that the code between the tags is a function call or function definition that will be used later.

Expressions can contain VB.NET code, for example:

graphic

<%
function HowOldAreYou(Birth as DateTime) as Integer
Dim Age as TimeSpan = Now() - Birth

HowOldAreYou = Age.Days /  365
end function
%>

This is normal text that will be written to the file as well as the following line with my age:
My age is: <%=HowOldAreYou("1980-05-05")%>

As you can the first expression tags contains the definition for a VB.NET function that calculates the age in years from an input date passed to the function. Later in the action the function is called from another expression that actually passes a date and returns the value in years that is written to the file as text.

Optitask uses the Microsoft .NET Framework to execute and parse expressions. Expressions from all the actions in a task are compiled into a .NET assembly that is loaded when the task executes to enable fast parsing of expressions in text fields.  Because the expressions is already in a binary computer language format you can add  complex and large expression functions inside text areas without worrying about the time it will take to execute and parse the expressions.

Function definitions like the HowOldAreYou function above can be used in any action in a task because all the functions are compiled in the same assembly.

Another handy feature of Optitask expressions is the ability to invoke code from external .NET assemblies that integrate directly with your Line of Business application. For more information about this see Calling .NET Assemblies.


The next step: Learn how to use Variables