Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about the Argument< T > and Variable< T > in .NET 4.0 Workflow Foundation

I am using Windows Workflow Foundation in .NET 4.0. Below is some syntax/semantic confusion I have.

I have 2 equivalent way to declare an Assign activity to assign a value to a workflow variable (varIsFreeShipping).

(1) Using XAML in the designer.

enter image description here

(2) Using code.

enter image description here

But in approach 2, the it seems I am creating a new OutArgument< Boolean > and assign value to it, not to the original Variable< Boolean> varIsFreeShipping. And OutArgument and Variable are totally different types.

So how could the value assigned to this new Argument finally reach the original Variable?

This pattern seems common in WF 4.0. Could anybody shed some light on this?

Thanks!

like image 439
smwikipedia Avatar asked Oct 02 '22 23:10

smwikipedia


1 Answers

As a matter of fact, the second (2) method can be written just as:

Then = new Assign<bool>
{
    To = varIsFreeShipping,
    Value = true
}

This all works because OutArgument<T> can be initialized through a Variable<T> using an implicit operator.

In your first (1) assign, using the editor, that's what's happening behind the scene; the variable is being implicitly converted from Variable to OutArgument.

WF4 uses alot of implicit operators mainly on Activity<T> from/to Variable<T>, OutArgument<T> from/to Variable<T>, etc. If you look at it, they all represent a piece of data (already evaluated or not), that is located somewhere. It's exactly the same as in C#, for example:

public int SomeMethod(int a)
{
    var b = a;
    return a;
}

You can assign an argument to a variable, but you can also return that same variable as an out argument. That's what you're doing with that Assign<T> activity (using the variable varIsFreeShipping as the activity's out argument).

This answers your question?

like image 64
Joao Avatar answered Oct 07 '22 17:10

Joao