locked
How to Convert Variable<int> to int? RRS feed

  • Question

  • Does anybody know how to convert a Variable<int> variable to int?

    Thanks in advance

    Thursday, November 24, 2011 9:41 PM

Answers

  • Hi,

    I'd suggest you use required argument in your custom activity. Since your custom activity is not a composite activity, it can derive from CodeActivity.

    public class CreateTaskActivity : CodeActivity
    {
    [RequiredArgument]
    public InArgument<Task> _task { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
    // Obtain the runtime value of the task input argument
    Task task = context.GetValue(this._task);

    DataModelDataContext _context = new DataModelDataContext();

    _context.Tasks.InsertOnSubmit(new Task() { AssignedTo = task.AssignedTo, Title = task.Title, StartDate = task.StartDate, DueDate = task.DueDate });
    _context.SubmitChanges();
    }
    }

    Then the code about CreateTaskActivity in your original post will be:

    new CreateTaskActivity()
    {
    task=new InArgument<task>(env=>tasks[contador.Get(env)])
    },

    Thanks.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by LeoTang Wednesday, November 30, 2011 2:31 AM
    • Proposed as answer by LeoTang Thursday, December 1, 2011 9:50 AM
    • Marked as answer by Rafael Mascarenhas Thursday, December 1, 2011 4:59 PM
    Tuesday, November 29, 2011 1:07 PM

All replies

  • It seems that I can use the Get method of my variable but it asks for ActivityContext. Any clues how to retrieve that on the code bellow??

     

    List<Task> tasks = GetTasks();
    
                var contador = new Variable<int>("contador", 0);
                
                string sCondition = "contador < " + tasks.Count.ToString();
    
                var vbvalue = new VisualBasicValue<int>("contador");
    
                While whileActivity = new While
                {
                    Variables = { contador },
                    DisplayName = "WhileCriaTarefa",
                    Condition = new VisualBasicValue<bool>(sCondition),
                    Body = new Sequence
                    {
                        DisplayName = "SequenceCriaTarefa",
                        Activities = 
                        {  
                             new CreateTaskActivity(tasks[contador.Get(????????)]),
                             new Assign<int>
                             {
                                  DisplayName = "Incrementa o contador",
                                  To=contador,
                                  Value = new VisualBasicValue<int>("contador + 1")
                             },
                             new WriteLine
                             {
                                   Text= new VisualBasicValue<string>("cstr(contador)")
                             }
                        }
                    }
                };
    
                lstActivities.Add(whileActivity);
    


    Thursday, November 24, 2011 10:03 PM
  • Hi,

    It can be done by using expression:

    new CreateTaskActivity(env=>tasks.Get(env)[contador.Get(env)]),

    Below is an article about data in Workflow 4, for your reference:

    Data and Windows Workflow Foundation 4

    http://msmvps.com/blogs/theproblemsolver/archive/2009/07/20/data-and-windows-workflow-foundation-4.aspx

    Thanks.

     


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by LeoTang Monday, November 28, 2011 2:18 AM
    Monday, November 28, 2011 2:18 AM
  • Thanks for the reply.

    It didn't work, here is the code for my CreateTaskActivity. There is no Get Method in my class. Should I implement it myself?

    Thank you again

    public class CreateTaskActivity : Activity
        {
            public CreateTaskActivity(Task task)
            {
                DataModelDataContext context = new DataModelDataContext();
                context.Tasks.InsertOnSubmit(new Task() { AssignedTo = task.AssignedTo, Title = task.Title, StartDate = task.StartDate, DueDate = task.DueDate });
                context.SubmitChanges();
            }
        }
    


    Monday, November 28, 2011 5:50 PM
  • Hi,

    I'd suggest you use required argument in your custom activity. Since your custom activity is not a composite activity, it can derive from CodeActivity.

    public class CreateTaskActivity : CodeActivity
    {
    [RequiredArgument]
    public InArgument<Task> _task { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
    // Obtain the runtime value of the task input argument
    Task task = context.GetValue(this._task);

    DataModelDataContext _context = new DataModelDataContext();

    _context.Tasks.InsertOnSubmit(new Task() { AssignedTo = task.AssignedTo, Title = task.Title, StartDate = task.StartDate, DueDate = task.DueDate });
    _context.SubmitChanges();
    }
    }

    Then the code about CreateTaskActivity in your original post will be:

    new CreateTaskActivity()
    {
    task=new InArgument<task>(env=>tasks[contador.Get(env)])
    },

    Thanks.


    Leo Tang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by LeoTang Wednesday, November 30, 2011 2:31 AM
    • Proposed as answer by LeoTang Thursday, December 1, 2011 9:50 AM
    • Marked as answer by Rafael Mascarenhas Thursday, December 1, 2011 4:59 PM
    Tuesday, November 29, 2011 1:07 PM
  • Thank you for your answer, that works like a charm

    Rafael

    Thursday, December 1, 2011 5:00 PM