title: Programmatically creating an Assign in a Flowchart Workflow
body: <p>I need to programmatically define a serializable flowchart Windows Workflow that accepts input arguments and returns a result.  I understand how to create these workflows using a designer, but I need to do it in code and have the flowchart workflow be serializable (so no lambda expressions).</p>
  
  <p>I'm having trouble getting the "To" field of the Assign.  The code below creates a flowchart workflow of a WriteLine followed by an Assign.</p>
  
  <pre><code>var ab = new ActivityBuilder&lt;string&gt; {
      Name = "ActivityBuilt",
      Implementation = new Flowchart {
          StartNode = new FlowStep {
              Action = new WriteLine { Text = new VisualBasicValue&lt;string&gt;("greeting") },
              Next = new FlowStep {
                  Action = new Assign {
                      DisplayName = "Set result",
                      To = new OutArgument&lt;string&gt;(new VisualBasicReference&lt;string&gt; { 
                          ExpressionText = "results"}),
                      Value = new VisualBasicValue&lt;string&gt;("bye")
                  }
              }
          }
      }
  };
  
  ab.Properties.Add(new DynamicActivityProperty {
      Name = "greeting", 
      Type = typeof (InArgument&lt;string&gt;),  
      Value = "hello"});
  ab.Properties.Add(new DynamicActivityProperty {
      Name = "results",  
      Type = typeof (OutArgument&lt;string&gt;), 
      Value = "bye"});
  
  // Convert the ActivityBuilder to a callable activity
  using (var sw = new StringWriter()) {
      using (var xw = ActivityXamlServices.CreateBuilderWriter(new XamlXmlWriter(sw, new XamlSchemaContext()))) {
          XamlServices.Save(xw, LastCreated);
      }
  
      using (var sr = new StringReader(sw.ToString())) {
           var wf = ActivityXamlServices.Load(sr);
           return wf;
      }
  }
  </code></pre>
  
  <p>The above code fails when I try to convert from ActivityBuilder to Activity saying "Failed to create a 'OutArgument' from the text 'bye'."  This works fine if I don't use the OutArgument and just pass things in.</p>
  
  <p>My question is what to put in the To property?  How do I reference the OutArgument I add to the ActivityBuilder.Properties?  A VisualBasicReference isn't an OutArgument.  Am I making this more difficult than it needs to be?</p>
  
  <p>Thanks for any hints!</p>
  
  <p>Edit: I want to create a workflow programmatically.  The workflow needs to have input arguments and return results (output arguments).  </p>
  
  <p>I understand how to create the workflow and how to declare and use input arguments.  I'm using an ActivityBuilder to create the workflow and to set the InArgument via the ActivityBuilder's properties.  I create the workflow from the ActivityBuilder by serializing to XAML and then deserializing using ActivityXamlServices.Load.  </p>
  
  <p>What I don't understand is how to get a result from the workflow.  I assume it involves an OutArgument.  How/where do I add an OutArgument to the workflow?  I thought the code snippet I gave would assign a value to an OutArgument, but the call to ActivityXamlServices.Load fails with a complaint that it is unable to create the OutArgument.  </p>
  
  <ul>
  <li>Is the approach of using ActivityBuilder correct?</li>
  <li>Is the "To" field of the Assign action properly referencing an OutArgument?</li>
  <li>How do I make the ActivityBuilder aware of the OutArgument and still be able to convert to an Activity / workflow?</li>
  </ul>
  
  <p>Hope this clarifies my problem.</p>