none
How to assign the value of my custom activity to the variable of the parent activity?

    Question

  • How to assign the value of my custom activity to the variable of the parent activity?

    You can see the code:

    public class ExecuteService : NativeActivity
    {
    public OutArgument Result
    {
    get;
    set;
    }

    public ExecuteService()
    {
    }

    protected override void Execute(NativeActivityContext context)
    {
    //assign the value to the variable of the parent activity
    }
    }

    public class Test
    {
    static Activity CreateActivity()
    {

    //Variable
    var FirstName = new Variable<System.String>();
    var LastName = new Variable<System.String>();

    //FlowStep
    ExecuteService ExecuteServiceObject1 = new ExecuteService()
    {
    Result = new System.Activities.OutArgument<System.String>(FirstName)
    };
    FlowStep FlowStepObject1 = new FlowStep() { Action = ExecuteServiceObject1 };

    ExecuteService ExecuteServiceObject2 = new ExecuteService()
    {
    Result = new System.Activities.OutArgument<System.String>(LastName)
    };
    FlowStep FlowStepObject2 = new FlowStep() { Action = ExecuteServiceObject2 };

    FlowStepObject1.Next = FlowStepObject2;

    //Flowchart
    Flowchart FlowChartObject1 = new Flowchart()
    {
    Variables =
    {
    FirstName,

    LastName
    },

    StartNode = FlowStepObject1,
    Nodes =
    {
    FlowStepObject1,
    FlowStepObject2
    }
    };

    Activity activity = new DynamicActivity()
    {
    Properties =
    {
    },
    Implementation = () => FlowChartObject1
    };
    return activity;
    }

    public static void RunWorkFlow()
    {
    WorkflowApplication application = new WorkflowApplication(CreateActivity());
    application.Run();
    }
    }

     

    I want to get the FirstName and LastName via the ExecuteService custom activity.The FirstName and LastName are variables of the FlowChart activity .

    Thursday, April 7, 2011 12:50 AM

Answers

  • You cannot get the variables FirstName and LastName by the variable name because they're not in the scope of your custom activity. Instead You can access the variables by the OutArgument Result since you assign the Result in the parent FlowChart activity:

    //FlowStep
    ExecuteService ExecuteServiceObject1 = new ExecuteService()
    {
    Result = new System.Activities.OutArgument<System.String>(FirstName)
    };

    You need to set Result in your custom activity ExecuteService like this:

    protected override void Execute(NativeActivityContext context)
    {
        //assign the value to the variable of the parent activity

        this.Result.Set(context, "anything");
    }

    BTW, You also need to bind a type for the OutArgument:

    public OutArgument<string> Result
    {
    get;
    set;
    }

    Wednesday, April 13, 2011 8:54 AM
    Moderator

All replies

  • Any advice?
    Friday, April 8, 2011 12:48 AM
  • I'm not sure I understand your question. Do you want to set the return value of your ExecuteService activity? Here's the code.

    public class ExecuteService : NativeActivity
    {
        public OutArgument<object> Result { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            this.Result.Set(context, "your result");
        }
    }

    Thanks,
    Tony

    Wednesday, April 13, 2011 2:34 AM
  • You cannot get the variables FirstName and LastName by the variable name because they're not in the scope of your custom activity. Instead You can access the variables by the OutArgument Result since you assign the Result in the parent FlowChart activity:

    //FlowStep
    ExecuteService ExecuteServiceObject1 = new ExecuteService()
    {
    Result = new System.Activities.OutArgument<System.String>(FirstName)
    };

    You need to set Result in your custom activity ExecuteService like this:

    protected override void Execute(NativeActivityContext context)
    {
        //assign the value to the variable of the parent activity

        this.Result.Set(context, "anything");
    }

    BTW, You also need to bind a type for the OutArgument:

    public OutArgument<string> Result
    {
    get;
    set;
    }

    Wednesday, April 13, 2011 8:54 AM
    Moderator