Task 1: Create the Basic Version of the Application

Download sample

In this task, you create the project that is used throughout the remainder of this tutorial, and implement the basic version of the application.

Create the Project

To create the RulesAndConditionsTutorial project

  1. In Visual Studio, click the File menu and choose New, Project. If you want to create a C# project, select Visual C#, Workflow. If you want to create a Visual Basic project, choose Visual Basic, Workflow. Choose Sequential Workflow Console Application. Name the project RulesAndConditionsTutorial, and click OK.

Note

Depending on which code language is specified as the default, the Visual C# or the Visual Basic project types may be displayed under the Other Languages node in the New Project dialog.

When the project is created, a workflow named Workflow1 is created, and this workflow is displayed in Visual Studio. If it is not displayed, right-click Workflow1.cs or Worflow1.vb in Solution Explorer and choose View Designer to open the workflow in design view. If the Solution Explorer is not displayed, choose Solution Explorer from the View menu.

Add and Initialize Workflow Members

To implement the first part of the game, variables are required for the random number, the user's guess, and number of turns. The range for the random number must also be defined.

To add the workflow members

  1. Open Workflow1 in code view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Code).

  2. Add the following member declarations to the Workflow1 class.

    public const int MaxNumber = 100;
    public int TargetNumber;
    public int Guess;
    public int Turns;
    

When the workflow starts the workflow's Initialized event is handled to initialize the game variables and display a message to the user.

To create the Initialized event handler

  1. Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).

  2. If the Properties window is not displayed, choose Properties Window from the View menu.

  3. Click the Workflow1 design surface to select it.

  4. Click the Initialized property in the Properties window, type GameStarted and then press ENTER.

When you press ENTER, a handler for the Initialized event is created and Visual Studio switches to the code view so that you can edit the newly-generated event handler.

To initialize the game variables

  1. Enter the following code into the GameStarted event handler.

    // Generate the Random number.
    TargetNumber = new Random().Next(1, MaxNumber + 1);
    
    // Initialize the game variables.
    Turns = 0;
    Guess = 0;
    
    // Prompt the user
    Console.WriteLine("Please guess a number from 1 to {0}", MaxNumber);
    

Add and Configure Workflow Activities

The game loop is implemented by a WhileActivity activity. This WhileActivity activity contains a SequenceActivity activity that contains a CodeActivity activity that has code to get the user's guess, and an IfElseActivity with two branches that provide feedback about whether the guess is too low or too high.

To add the game loop activities

  1. Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).

  2. If the Toolbox is not visible, choose Toolbox from the View menu.

  3. If no workflow activities are displayed in the Toolbox, expand the Windows Workflow v3.0 node.

  4. Drag a While activity from the Toolbox and drop it onto the workflow.

  5. Drag a Sequence activity from the Toolbox and drop it inside the While activity.

  6. Drag a Code activity from the Toolbox and drop it inside the Sequence activity.

  7. Double-click the Code activity and enter the following code into the generated event handler.

    bool validEntry = false;
    while (!validEntry)
    {
        validEntry = true;
        Console.Write("Enter your guess: ");
        string s = Console.ReadLine();
    
        if (!Int32.TryParse(s, out Guess))
        {
            Console.WriteLine("Please enter an integer.");
            validEntry = false;
        }
    }
    
    // Increment the number of Turns
    Turns++;
    
  8. Switch back to the workflow design view.

  9. Drag an IfElse activity from the Toolbox and drop it into the Sequence activity so that it follows the Code activity.

By default, an IfElseActivity activity has two IfElseBranchActivity activities. The left branch is used to report if the user's guess is too low and the right branch is used if the user's guess is too high.

To configure the IfElse activity

  1. Drag a Code activity from the Toolbox and drop it into the left branch of the IfElse activity.

  2. Double-click the Code activity and enter the following code into the generated event handler.

    Console.WriteLine("Your guess was too low. Please try again.");
    
  3. Switch back to the workflow design view.

  4. Drag a Code activity from the Toolbox and drop it into the right branch of the IfElse activity.

  5. Double-click the Code activity and enter the following code into the generated event handler.

    Console.WriteLine("Your guess was too high. Please try again.");
    
  6. Switch to the workflow design view.

Note the red exclamation point that is displayed on the left IfElseBranch activity. This indicates that the condition has not been set for this branch.

To configure the left IfElseBranch activity

  1. Click the exclamation point and from the context menu that is displayed choose Property 'Condition' is not set, which shifts the focus to the Condition property in the Properties window.

  2. Click the drop-down arrow beside the Condition property and choose Declarative Rule Condition.

  3. Expand the plus sign by the Condition property that appeared when Declarative Rule Condition was selected in the previous step.

  4. Enter TooLowCondition as the value for the ConditionName property.

  5. Click the Expression property to select it, and then click the ellipsis button that is displayed to the right of the Expression property.

  6. Enter the following expression and click OK.

    this.Guess < this.TargetNumber
    

Note

Declarative activity conditions are parsed into System.CodeDom expressions and are not compiled. Although the previous expression resembles a C# statement it is the correct expression for use in both Visual C# and Visual Basic projects.

The right-most branch of an IfElseActivity activity can be configured like an else or an else if. In this application, a declarative rule condition is applied that configures the branch as an else if. Because it is optional for the right-most branch of an IfElseActivity activity to have a condition, there is no red exclamation indicator if no condition is present.

To configure the right IfElseBranch activity

  1. Right-click the right-most IfElseBranch activity and choose Properties from the context menu.

  2. Click the drop-down arrow beside the Condition property and select Declarative Rule Condition.

  3. Expand the plus sign by the Condition property that appeared when Declarative Rule Condition was selected in the previous step.

  4. Enter TooHighCondition as the value for the ConditionName property.

  5. Click the Expression property to select it, and then click the ellipsis button that is displayed to the right of the Expression property.

  6. Enter the following expression and click OK.

    this.Guess > this.TargetNumber
    

Note that the While activity has an error indicator displayed as well. This is because a condition is required. This condition is evaluated when the activity is initially executed, and while the condition evaluates to true, the WhileActivity activities' child activity executes. For this application, a CodeCondition is used to determine when the game loop should complete.

To configure the condition for the While activity

  1. Right-click the While activity and choose Properties from the context menu.

  2. Click the drop-down arrow beside the Condition property and choose Code Condition.

  3. Expand the plus sign by the Condition property that appeared when Code Condition was selected in the previous step.

  4. Enter GameLoopCondition as the value for the newly expanded Condition property and press ENTER.

  5. Enter the following code in the generated handler.

    // If Guess == TargetNumber then we do not want
    // to continue. Set Result to true to continue
    // iterating, and false to exit the loop.
    
    e.Result = Guess != TargetNumber;
    

When the user's guess matches the target number, the WhileActivity activities' condition evaluates to false and the WhileActivity activity completes. At this point, the game is over so the workflow can be configured to display some statistics.

To display game statistics

  1. Switch to the workflow design view.

  2. Drag a Code activity from the Toolbox and drop it so that it is the last activity in the workflow.

  3. Double-click the Code activity and enter the following code into the generated handler.

    Console.WriteLine("Congratulations, you guessed the number in {0} turns", Turns);
    

Build and Run the Application

Build and run the application and see how you do at guessing the number.

To build and run the application

  1. Press Ctrl+F5 to build and run the application.

In Task 2: Add the User Ranking to the Application, you add a PolicyActivity activity and configure a RuleSet to provide a ranking for the player.

See Also

Reference

Initialized
IfElseBranchActivity
IfElseActivity
CodeActivity
SequenceActivity
WhileActivity

Concepts

Workflow Authoring Styles

Other Resources

Task 2: Add the User Ranking to the Application
Developing Workflow-Enabled Applications

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04