[Translation] WF from entry to proficiency (1)

Source: Internet
Author: User
The following is a short section for postal code verification. Code

String uscode = @ "^ (\ D {5} $) | (\ D {5} $ \-\ D {4} $ )";
String canadiancode = @ "[abceghjklmnprstvxy] \ D [A-Z] \ D [A-Z] \ D ";

Public static bool validatepostalcode (string Str)
{
Return (RegEx. ismatch (STR, uscode) | RegEx. ismatch (STR, canadiancode ));
}

This is nothing special: "test an input string. If it is a U.S. zip code or a Canadian zip code, true is returned; otherwise, false is returned ". This is a very practical piece of code. In fact, if you don't want to use other validation controls in ASP. NET, you can use this verification logic in your ASP. NET. Create a workflow application.ProgramIt performs the same verification and returns information about whether the verification is successful.

Create a console-based workflow project

1. Start Visual Studio 2008
2. Create a sequential workflow console application project

3. Enter pcodeflow in the project name
4. Click OK to automatically generate the following initial interface.

Before editing the code or inserting a workflow entry, take a moment to see the two files generated by the workflow project for you:
Program. CS: It can be seen from many aspects that this is a typical console application source file. However, this template adds a lot of code to support workflow operations. Understanding the code is a major goal of this book, but this chapter just gives a brief introduction to what it has done.
Workflow1.cs: This is a workflow component. We will modify it to verify the zip code. You can put many things into it for the first time, but now we should start working with this simple workflow.

Create a Workflow

drag an ifelse Activity component from the toolbox to the workflow design interface.
now we will use the ifelse Activity component to ask the following question: "is one of our existing strings a legal zip code ?". In the code, we will actually use the regular expression in the code snippet you saw before to ask this question.
but before proceeding to this step, take a closer look at the view designer of workflow. The workflow view designer reminds us that no code is provided for this decision. If you look at the upper right corner of the left branch named "ifelsebranchactivity1", you will see a small circular icon marked with an exclamation point. This is why the Workflow view designer tells you that workflow is incomplete. If you try to compile a project with such a reminder icon, an error will be reported during compilation. If you move the mouse over the icon and click the downward arrow, you can see more information about this error.

Add code to the branch of the ifelse Activity
1. Move the mouse to the left branch named "ifelsebranchactivity1" and click to activate the property of this branch on the property panel.
2. We add a condition to force workflow to execute some actions when it is executed to the branch (when the condition is true, the left branch will be executed ). To do this, click the drop-down list of the activation condition type attribute of the "condition" attribute. From the list, you can select the "Code condition" type, "(none)" type, and "declarative rule condition" type. Select the "Code condition" type.
3. after the preceding steps are completed, the "condition" type property user interface will contain a "+". We click to expand its sub-attribute, which is also named "condition ", click to activate it.
4. For the "condition" attribute, enter the name of the Internal event we want to add. This event is triggered when conditions need to be determined. In this example, enter "evaluatepostalcode ".
Visual Studio 2008 adds the events you specify in the condition attribute to the workflow source file behind the scenes. We will add the Regular Expression Code segment to be executed when the event is triggered later.

Before doing this, let's continue to work on the Workflow view designer. We just added a condition that will cause the workflow to select the path on the left or right. However, the actions that the workflow will perform are not specified in both paths. We add activities to the "ifelsebranchactivity1" branch on the left and the "ifelsebranchactivity2" branch on the right.

Add code Activity
1. Drag a "code" activity to the Workflow view designer and place it in the area of the left branch (ifelsebranchactivity1.
2. Just like the code that previously added conditional judgment, we will add the code to be executed when the branch is selected. Click the "codeactivity1" icon to activate its properties in the property panel.
3. Enter "postalcodevalid" in the "executecode" attribute ".
Visual Studio 2008 automatically inserts the event. We will provide the corresponding Execution Code later. The right branch does the same. You only need to enter "postalcodeinvalid" in the "executecode" attribute ".

Add event processing code in our Workflow
1. Open workflow. CS to prepare for editing
2. Add reference: using system. Text. regularexpression;
3. Locate the "evaluatepostalcode" method and insert the following code:

Private   Void Evaluatepostalcode ( Object Sender, conditionaleventargs E)
{
String Uscode =   @" ^ (\ D {5} $) | (\ D {5} $ \-\ D {4} $) " ;
String Canadiancode =   @" [Abceghjklmnprstvxy] \ D [A-Z] \ D [A-Z] \ D " ; E. Result = (RegEx. ismatch (_ code, uscode) | RegEx. ismatch (_ code, canadiancode ));
}

Variable E is an instance of the "conditionaleventargs" type. It is used to tell the path to be selected for the "ifelse" activity.

4. We also need to add a capability for the workflow activity to accept input strings for verification. Add the following code to declare a public attribute named "postalcode.

Private   String _ Code = String . Empty;
Public   String Postalcode
{
Get
{
Return_ Code;
}
Set
{
_ Code=Value;
}
}

With this, our workflow application can be compiled, but the program is not complete. We also need to locate the "postalcodevalid" method in the workflow1.cs file and insert the following code:
Console. Write ("the postal code {0} is valid.", _ Code );
Insert the following code in the "postalcodeinvalid" method:
Console. Write ("the postal code {0} is * invalid *.", _ Code );

Call Workflow
1. Open the program. CS file.
2. Locate:
Workflowinstance instance = workflowruntime. createworkflow (typeof (pcodeflow. workflow1), wfargs );
3. Replace the above Code:

Dictionary < String , Object > Wfargs =   New Dictionary < String , Object > ();
Wfargs. Add ( " Postalcode " , ArgS. Length >   0   ? ARGs [ 0 ]: "" ); Workflowinstance instance = Workflowruntime. createworkflow ( Typeof (Pcodeflow. workflow1), wfargs );

Compile the application

Execute your workflow application
1. Open the Command Prompt window.
2. Locate the compiled application directory at the command prompt.
3. Enter pcodeflow 12345 to view the execution result.
4. Enter pcodeflow 1234x and check the execution result.

Download source code: pcodeflow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.