title: How to pass parameters to a CodeActivity in a NativeActivity code sequence
body: <p>I'm trying to get windows workflows working, and I've become a little stumped.</p>
  
  <p>I've gotten a single workflow working, but now I am trying to do something a little more complex: start a workflow, where each activity itself contains a workflow. (Picture something like the main program starts the activities "Input, logic, and output", and then each of those have additional activities like "prompt user, get input, etc.")</p>
  
  <p>I've had it working fine, with the example from here (<a href="http://msdn.microsoft.com/en-us/magazine/gg535667.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/magazine/gg535667.aspx</a>), when I am not passing any parameters from the main program to the activites. My question is, how exactly does the 'Variables' and 'metadata.SetVariablesCollection' work in the NativeActivity, and how to I get the parameters to the low level activities?</p>
  
  <p>This is what I am currently trying:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Activities;
  using System.Collections.ObjectModel;
  using System.Activities.Statements;
  
  namespace Project1
  {
      internal class MainProgram
      {
          internal static void Main(string[] args)
          {
              try
              {
                  var act = new SimpleSequence();
  
                  act.Activities.Add((Activity)(new WriteSomeText()));
                  act.Activities.Add((Activity)(new WriteSomeText()));
                  act.Activities.Add((Activity)(new WriteSomeText()));
  
                  act.Variables.Add(new Variable&lt;string&gt; ("stringArg", "TEXT"));
  
                  WorkflowInvoker.Invoke(act);
              }
              catch (Exception ex)
              {
                  System.Console.WriteLine("EXCEPTION: {0}", ex);
              }
          }
  
          public class WriteSomeText : CodeActivity
          {
              [RequiredArgument]
              public InArgument&lt;string&gt; stringArg { get; set; }
  
              protected override void Execute(CodeActivityContext context)
              {
                  string output = context.GetValue(stringArg);
                  System.Console.WriteLine(output);
              }
  
          }
  
          public class SimpleSequence : NativeActivity
          {
              Collection&lt;Activity&gt; activities;
              Collection&lt;Variable&gt; variables;
  
              Variable&lt;int&gt; current = new Variable&lt;int&gt; { Default = 0 };
  
              public Collection&lt;Activity&gt; Activities
              {
                  get
                  {
                      if (this.activities == null)
                          this.activities = new Collection&lt;Activity&gt;();
  
                      return this.activities;
                  }
                  set
                  {
                      this.activities = value;
                  }
              }
  
              public Collection&lt;Variable&gt; Variables
              {
                  get
                  {
                      if (this.variables == null)
                          this.variables = new Collection&lt;Variable&gt;();
  
                      return this.variables;
                  }
                  set
                  {
                      this.variables = value;
                  }
              }
  
              protected override void CacheMetadata(NativeActivityMetadata metadata)
              {
                  metadata.SetChildrenCollection(this.activities);
                  metadata.SetVariablesCollection(this.variables);
                  metadata.AddImplementationVariable(this.current);
              }
  
              protected override void Execute(NativeActivityContext context)
              {
                  if (this.Activities.Count &gt; 0)
                      context.ScheduleActivity(this.Activities[0], onChildComplete);
              }
  
              void onChildComplete(NativeActivityContext context, ActivityInstance completed)
              {
                  int currentExecutingActivity = this.current.Get(context);
                  int next = currentExecutingActivity + 1;
  
                  if (next &lt; this.Activities.Count)
                  {
                      context.ScheduleActivity(this.Activities[next], this.onChildComplete);
  
                      this.current.Set(context, next);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This ends up throwing the following exception:</p>
  
  <pre><code>EXCEPTION: System.Activities.InvalidWorkflowException: The following errors were encountered while processing the workflow tree:
  'WriteSomeText': Value for a required activity argument 'stringArg' was not supplied.
  'WriteSomeText': Value for a required activity argument 'stringArg' was not supplied.
  'WriteSomeText': Value for a required activity argument 'stringArg' was not supplied.
     at System.Activities.Validation.ActivityValidationServices.ThrowIfViolationsExist(IList`1 validationErrors)
     at System.Activities.Hosting.WorkflowInstance.ValidateWorkflow(WorkflowInstanceExtensionManager extensionManager)
     at System.Activities.Hosting.WorkflowInstance.RegisterExtensionManager(WorkflowInstanceExtensionManager extensionManager)
     at System.Activities.WorkflowApplication.EnsureInitialized()
     at System.Activities.WorkflowApplication.RunInstance(WorkflowApplication instance)
     at System.Activities.WorkflowApplication.Invoke(Activity activity, IDictionary`2 inputs, WorkflowInstanceExtensionManager extensions, TimeSpan timeout)
     at System.Activities.WorkflowInvoker.Invoke(Activity workflow, TimeSpan timeout, WorkflowInstanceExtensionManager extensions)
     at System.Activities.WorkflowInvoker.Invoke(Activity workflow)
     at Project1.MainProgram.Main(String[] args) in c:\users\user\documents\visual studio 2010\Projects\ModelingProject1\Project1\MainProgram.cs:line 25
  </code></pre>
  
  <p>I know, I only pass 1 parameter, but the exception still says that I am missing 3 parameters. I am missing something as to how to do this properly.</p>