WF4 – Guess the number game!
Posted
by MarkPearl
on Geeks with Blogs
See other posts from Geeks with Blogs
or by MarkPearl
Published on Thu, 15 Apr 2010 04:12:23 GMT
Indexed on
2010/04/15
5:33 UTC
Read the original article
Hit count: 263
I posted yesterday how really good WF4 was looking. Today I thought I would show some real basics that I was able to figure out.
This will be a simple example, I am going to make a flowchart workflow – which will prompt the user to guess the number until they guess the right number.
Lets begin…
Make a new project and make it a Workflow console Application.
Then select the Workflow file and drag a FlowChart (2) to point 3. This will now show a green start circle in the designer form. We are going to work with primitives to start with.
We are now going to drag a few objects onto the Workflow, We drag the WriteLine, Assign & Decision items onto the designer. Once they are dragged onto the designer we will want to link them up. The order that they are linked is critical since this will determine the order of the solution.
In this case, we want the system to first ask “Guess a number”, then to wait for the user to input some code, and then to display “You got it” if they got it right, and “Try again” if they got it wrong.
So we now link the arrows to the objects. This is done by moving the mouse pointer over the start objects and clicking on one of the toggles and then dragging it to the next object and releasing the button over one of the toggles. This will place an arrow from the source object to the target object.
Okay… pretty simple stuff – now we just need these primitive objects to do stuff.
Lets start with the WriteLine primitive.
We place the text in inverted commas in the Text field. Because this field accepts any valid VB expression we could have put variables etc. in there if we wanted to.
The next thing we want to do is allow the user to input a number. This brings up an interesting problem, if a user were to type in a number, there would need to be someway to declare a variable to hold that value for the life of the workflow. We can achieve this by declaring a variable.
To declare a variable, move your cursor over the variables tab at the bottom of the workflow, and then type the name of the new variable in the “Create Variable” field and set it as shown in the image above.
Now that we have a variable, we want to call the Console.Readline method and assign the inputted value from the Console to that variable.
The code that cannot be seen is actually this – Convert.ToInt32(Console.ReadLine())
We now have a workflow that first prompts the user for a number, then allows the user to type in a number. We are almost done, we just need to make the system react to the value inputted. There are a few ways we could do this, I am going to use the Decision item.
So select the Decision object on the designer and then view its properties (F4 for me), and in the condition field place a condition. For simplicity sake I have decided that if the user guesses 10, they will have guessed the number.
This is now the completed workflow. Its really easy to understand and shows some really powerful principles for Business applications. You can run the application and see what it does.
Imagine writing business solutions that do not worry about the exact flow of objects, but simply allows a business analyst or someone to configure the solution to work exactly as the business rules would dictate. And if the rules changed six months later all they would need to do is re-drag some of the flows.
Now I do not know if WF4 will allow for this, but it feels like it is a step in the right direct.
© Geeks with Blogs or respective owner