Search code examples
.networkflow-foundation-4workflow-foundation

Using ByRef variables in Windows Workflow Foundation?


Lets say I create a simple workflow containing two variables, result of type Boolean and myInt of type Integer. Now add an activity "Assign", placing result in the result box, and Integer.TryParse("22", myInt) in the right hand expression. After running this activity, the variable still has the value 0.

Why is the result of the TryParse call not correctly stored in the variable? (No errors are generated here either)


Solution

  • That's not how WF works. Variables don't have the concept of in/out as arguments. They don't implement implicit operators so the result will never be stored as you wish.

    Either you implement your own TryParse activity or you can use InvokeMethod like this:

    var resultVar = new Variable<bool>("result");
    var myIntVar = new Variable<int>("myInt");
    
    var activity = new Sequence
    {
        Variables = 
        {
            resultVar,
            myIntVar
        },
        Activities =
        {
            new InvokeMethod
            {
                TargetType = typeof(int),
                MethodName = "TryParse",
                Result = new OutArgument<bool>(resultVar),
                Parameters = 
                {
                    new InArgument<string>("22"),
                    new OutArgument<int>(myIntVar)
                }
            },
            new WriteLine
            {
                Text = new VisualBasicValue<string>(@"""INT: "" & myInt")
            }
        }
    };