Task 1: Create the Basic Version of the Application
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
- 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
Open
Workflow1
in code view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Code).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
Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).
If the Properties window is not displayed, choose Properties Window from the View menu.
Click the Workflow1 design surface to select it.
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
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
Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).
If the Toolbox is not visible, choose Toolbox from the View menu.
If no workflow activities are displayed in the Toolbox, expand the Windows Workflow v3.0 node.
Drag a While activity from the Toolbox and drop it onto the workflow.
Drag a Sequence activity from the Toolbox and drop it inside the While activity.
Drag a Code activity from the Toolbox and drop it inside the Sequence activity.
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++;
Switch back to the workflow design view.
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
Drag a Code activity from the Toolbox and drop it into the left branch of the IfElse activity.
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.");
Switch back to the workflow design view.
Drag a Code activity from the Toolbox and drop it into the right branch of the IfElse activity.
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.");
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
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.
Click the drop-down arrow beside the Condition property and choose Declarative Rule Condition.
Expand the plus sign by the Condition property that appeared when Declarative Rule Condition was selected in the previous step.
Enter TooLowCondition as the value for the ConditionName property.
Click the Expression property to select it, and then click the ellipsis button that is displayed to the right of the Expression property.
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
Right-click the right-most IfElseBranch activity and choose Properties from the context menu.
Click the drop-down arrow beside the Condition property and select Declarative Rule Condition.
Expand the plus sign by the Condition property that appeared when Declarative Rule Condition was selected in the previous step.
Enter TooHighCondition as the value for the ConditionName property.
Click the Expression property to select it, and then click the ellipsis button that is displayed to the right of the Expression property.
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
Right-click the While activity and choose Properties from the context menu.
Click the drop-down arrow beside the Condition property and choose Code Condition.
Expand the plus sign by the Condition property that appeared when Code Condition was selected in the previous step.
Enter GameLoopCondition as the value for the newly expanded Condition property and press ENTER.
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
Switch to the workflow design view.
Drag a Code activity from the Toolbox and drop it so that it is the last activity in the workflow.
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
- 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
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