Adding Variables, Expressions and Bindings Programmatically

I am assuming we already know how to add activities on to the designer surface dynamically using the designer programmatic APIs. If not please take a look here.

Another variation of the ask is we want to add variables and arguments to our activities/workflows at design time dynamically. Example scenario: If a specific activity is drag dropped and its input type is same as the output of the previous activity on the workflow, bind the input of this newly added activity to the output of the other activity.

Yes, we would need a variable here so that we can bind it to the InArgument and the OutArgument of the activities to link them for data flow.

How do we go about doing it:

const string myStrName = "myStrName";
Variable<string> mySimpleVar= new Variable<string> { Name = myStrName };

This was just about adding a variable. How about providing a default value to the variable. Lets  see how we go about doing that:

mySimpleVar.Default = new VisualBasicValue<string>
      ExpressionText = "\"Kushal\"",

Next step/hurdle – What if the variable is of some user defined type? We would then need to add a namespace declaration as part of the Imports section in the Workflow

Example: If you want to create a variable of type fooClass defined in assembly – fooAssembly.dll and namespace - foo, we would go about as follows:

static string requiredAssemblyName = 

static string requiredNamespace = typeof(foo.fooClass).Namespace;
VisualBasicSettings vbSettings = new VisualBasicSettings();
vbSettings.ImportReferences.Add(new VisualBasicImportReference
        Assembly = requiredAssemblyName,
        Import = requiredNamespace

mySimpleVar.Default = new VisualBasicValue<foo.fooClass>
       ExpressionText = "new foo.fooClass()",
         Settings = vbSettings


Now the rest is about the binding of the outArgument of one activity to the inArgument of the other activity. For example:

Argument myArg = 
myArg.Expression =   new VisualBasicValue<foo.fooClass>
                        ExpressionText = myStrName


where “Response” is an InArgument for my activity and myStrName is the variable we just created above.

We follow the similar model for the outArgument and you have programmatically completed binding the output of one activity to the input of another activity. One change though is that for the outArgument expression assignment you need to use a VisualBasicReference instead of a VisualBasicValue.




Comments (6)
  1. Lixiang says:

    I tried your sample code and adding variable to In argument is successful. But when I tried the same with an Out Argument, I got an error when serializing the workflow using XamlServices. Could you please show an example on how to bind an variable to an out argument?


  2. Bob Ansari says:

    Use following method to set compiler settings:

    VisualBasic.SetSettings(vb, vbSettings);    

  3. Jason Bolstad says:

    Hi Kushal

    I was trying to autobind when an activty with an inArgument<string> is added into a ForEachActivity<String>.

    The issue is that the ForEach<string> allows the user to name the item variable.  But I can't for the life of me find the variable name in the forEach's modelItem.

    If I had this then the binding would be as simple as creating an expression of inArgument<string> with the item varaible name, but instead I'm stuck.

    The ForEach<string> activities "Body" property (ActivityAction<T>) shows as null in the model item.

    Am I being an idiot here, or is there another way to do this.

    Cheers, Jason

  4. eric says:

    There is no Settings property defined in VisualBasicValue.

  5. Bandu says:

    This is exactly what i'm looking for. But where should I put those code in? In my custom activity code? I think I need to put it inside my custom activity code but I can't find Model Item.

  6. Dave says:

    When I run this code in visual studio 2010 and the following line is executed:


    it caused the variables pane to be visible even though I have WorkflowShellBarItemVisibility = ShellBarItemVisibility.MiniMap | ShellBarItemVisibility.Zoom

    Do you know what could be causing this?

Comments are closed.

Skip to main content