Exercise 7: If/Else Logic

In the previous exercise, you created an enhanced Hello Workflow application with a custom hello message. In this exercise, you will add If/Else logic to the workflow to display a different Hello message depending on a custom condition.

This exercise will use the "write the test first" approach, that is, first writing a test for the new requirement and then implementing the necessary code to make it pass.

Task 0 – Opening the Solution

To begin this exercise you can use the solution you finished from Exercise 6. Alternatively, you can follow the following steps to begin with Exercise 7.

  1. Start Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010.
  2. Open the starting solution for Exercise 7 located under the \Source\Ex7-IfElse\Begin folder and use it as the starting point for this exercise.
  3. Press CTRL+SHIFT+B to build the solution.

Task 1 – Writing the Test for a New Requirement

You have a new requirement for your application. If the name has an odd number of letters then you want the first word of the greeting to be "Greetings", otherwise the first word should be "Hello". As an example, now your workflow will be the equivalent of this code:

C#

private static string SayHello(string userName) { string FirstWord = null; if (userName.Length % 2 == 0) FirstWord = "Hello"; else FirstWord = "Greetings"; return FirstWord + ", " + userName +" from Workflow 4"; }

Visual Basic

Private Shared Function SayHello(ByVal userName As String) As String Dim FirstWord As String = Nothing If userName.Length Mod 2 = 0 Then FirstWord = "Hello" Else FirstWord = "Greetings" End If Return FirstWord & ", " & userName & " from Workflow 4" End Function

  1. Create a test to verify the new requirement. To do this, open the SayHelloFixture.cs file (C#) or SayHelloFixture.vb (Visual Basic), located under the HelloWorkflow.Test project, and add the following test.

    (Code Snippet - Introduction to WF4Lab - ShouldReturnGreetingWithOddLengthName Test CSharp)

    C#

    [TestMethod] public void ShouldReturnGreetingWithOddLengthName() { var output = WorkflowInvoker.Invoke( new SayHello() { UserName = "Odd" }); string greeting = output["Greeting"].ToString(); Assert.AreEqual("Greetings Odd from Workflow 4", greeting); }
    FakePre-a00ef2946d274b619753df9685cb893f-f5f2b9c4db484e649efe2e5f23bc4670
    

    (Code Snippet - Introduction to WF4Lab - ShouldReturnGreetingWithOddLengthName Test VB)

    Visual Basic

    <TestMethod()> Public Sub ShouldReturnGreetingWithOddLengthName() Dim output = WorkflowInvoker.Invoke( New SayHello() With {.UserName = "Odd"}) Assert.AreEqual("Greetings Odd from Workflow 4", output("Greeting").ToString()) End Sub

  2. Right-click over test method and select Run Tests (). The test will fail because you have not modified the workflow yet to return a different hello message in each case.

    Figure 42

    ShouldReturnGreetingWithOddLengthName test failing

Task 2 – Implementing the New Requirement in the Workflow

  1. Open SayHello.xaml in the workflow designer. To do this, double-click the file in Solution Explorer.
  2. Add a FirstWord variable to store the first word of the hello message, whether it is "Hello" or "Greetings". To do this, follow these steps:

    1. Select the Sequence by clicking over the shape surface.
    2. Click the Variables button. A panel that displays the available variables for the Sequence activity appears.
    3. Click Create Variable.
    4. Type FirstWord in the Name box.

    Figure 43

    Adding the FirstWord variable of type String to Sequence

    Note:
    Variables

    In Windows Workflow Foundation (WF), variables represent the storage of data. Arguments, on the other side, represent the flow of data into and out of an activity. Variables have a scope just as they do in C# or Visual Basic. If you open the variables pane without selecting an activity you will not be able to add a variable. The activity selected in the designer will provide the scope for the variable. In this case, FirstWord belongs to the Sequence scope.

  3. Now you need to test the UserName argument to see if it has an even or odd number of characters. To do this, drag an If activity from the Control Flow group in the toolbox and drop it into the Sequence activity above the Assign activity.
  4. Set the If activity DisplayName to If UserName is Even by selecting the text on the shape and editing it inline or by setting it in the property grid (select the activity and press F4 to display it).

    Note:
    The Workflow designer allows you to give the shape a more readable name by setting its DisplayName property.

    Figure 44

    The If activity with the description set

    Note:
    Watch Out

    The red icons () are warning you that this workflow is not valid currently. The If activity still requires that you supply an expression for the Condition argument.

  5. Supply an expression for the If condition. To do this, double-click the If activity to open it and type the following expression in the Condition box. This will check if the name length is odd or even.

    Visual Basic

    UserName.Length Mod 2 = 0

    Note:
    Expressions

    Expressions are program statements that can be a literal string, a conditional statement, or an expression that concatenates several strings or calls a method or invokes another activity. Expressions are written using Visual Basic syntax even if the application is in C#. This means capitalization does not matter, comparison is performed using a single equals sign instead of “==”, and the Boolean operators are the words "And" and "Or" instead of the symbols "&&" and "||".

  6. Update the FirstWord variable value for even length names, which should be greeted with a "Hello" message. Drag an Assign activity from Primitives group in the toolbox and drop it into the Then area. After that, type FirstWord in the To box (left side) and "Hello " (including the space at the end) in the Value box (right side).
  7. Now update the FirstWord variable value for odd length names, which should be greeted with a "Greetings" message. Drag and Assign activity from Primitives group in the toolbox and drop it into the Else area. Then type FirstWord in the To box (left side) and "Greetings " (including the space at the end) in the Value box (right side).

    Figure 45

    The completed If activity

  8. Modify the final Assign activity to customize the hello message displayed based on the FirstWord value. To do this, in the Assign activity located below the If activity replace the content of the Value box (right side) with the following expression:

    Visual Basic

    FirstWord & UserName _ & " from Workflow 4"

    Figure 46

    The Completed Workflow

  9. Press CTRL+SHIFT+B to build the solution.

Next Step

Exercise 7: Verification