First Program with WorkFlow (Part II)
Thursday, July 24, 2008You need to see the previous post to begin with. Now we will add the code.
To do so, click on Solution Explorer’s View Code toolbar button to open the Workflow1.cs C# file for editing. In the constructor InitializeComponent is called. InitializeComponent is there in WorkFlow1.designer.cs where it initializes the events we inserted using designer. We will locate EvaluatePostalCode method in Workflow1.cs and insert the following code.
string USCode = @"^(\d{5}$)|(\d{5}$\-\d{4}$)";
string CanadianCode =@"[ABCEGHJKLMNPRSTVXY]\d[A-Z]\d";
e.Result = (Regex.IsMatch(_code, USCode) || Regex.IsMatch(_code, CanadianCode));
here e is an instance of ConditionalEventArgs, used to tell the IfElse activity which branch to take.
e.Result = true, means left branch is executed
To accept the incoming string, we add public property.
private string _code = String.Empty;
public string PostalCode
{
get{ return _code;}
set{_code = value;}
}
At this point our code will compile but no result. We need to add handlers for the conditional branches. Write following line of code in PostalCodeValid method.
Console.WriteLine("The postal code {0} is valid.",_code);
Similarly, handler for right branch in PostalCodeInvalid method.
Console.WriteLine("The postal code {0} is invalid.",_code);
This is all, workflow portion is complete. Now we’ll add code to Main method for startup.
You will see the following code in Main method:
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(PCodeFlow.Workflow1));
Replace it by following.
Dictionary
wfArgs.Add("PostalCode", args.Length>0?args[0] : "");
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(PCodeFlow.Workflow1), wfArgs);
Autogenerated code would work fine, however we want to pass the postal code found on the command line so we have replaced it by our own code.
Now you can run it and type the following command pcodeflow 12345 and hit “Enter” key. You see the output as “The postal code 12345 was valid. You can check for invalid condition.
Woooo….w! Our first program is running.