System.Activities.Statements.If
- If activity contains a statement and two conditions.
- The first condition (the activity in the Then section) is executed if the statement is true, while the second one (the activity in the Else section) is executed if the statement is false.
Properties
Misc
- Condition (InArgument<Boolean>) – Define a condition/an expression which returns boolean result.
- DisplayName (String) – To set a meaningful name according to the user.
Example
To use an If activity, create an automation that asks user to enter two numbers, checks to see if one is divisible by the other, and depending on the result, displays a different message in the Output panel.
- Create a sequence.
- Create two integer variables, FirstNumber and SecondNumber.
- Add two Input Dialog
- In the Properties panel, type labels and titles for both activities and, in the Result fields, add the FirstNumber and SecondNumber
- Add an If activity under the previously added Input Dialog
- In the Condition field, type FirstNumber mod SecondNumber= 0. This expression checks if the first number is divisible to the second one, using the mod
- In the Then section, add a WriteLine
- In the Text field, type ToString+ “ is divisible by “ + SecondNumber.ToString+ “.”. The message displayed if the first number is divisible by the second one.
- In the Else section, add another WriteLine
- In the Text field, type ToString+ “ is not divisible by “ + SecondNumber.ToString+ “.”. The message displayed if the first number is not divisible by the second one.
- Run the process.
- During the execution, enter numbers when prompted.
- After the execution, the Output panel displays the result, depending on the values added in the Input Dialog