How to access an object list in a private method?

I need to acess a list of objects that in being instanced at the moment that the SharePoint Workflow is activated.

private void onWorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
{
    string json = @"[
        {
            'StateID': 1,
            'Title': 'State One'
        },
        {
            'StateID': 2,
            'Title': 'State Two'
        }
    ]";

    Json j = new Json();

    List<State> states = JsonConvert.DeserializeObject<List<State>>(json);
}

I need to be able to access the list when I change to another step of the workflow by doing similar to the following:

private void StateOneTask_MethodInvoking(object sender, EventArgs e)
{
    try
    {
        StateOneTask_TaskId = Guid.NewGuid();
        StateOneTask_ContentTypeId = TaskContentType;
        createStateOneTask.TaskProperties = new SPWorkflowTaskProperties();
        createStateOneTask.TaskProperties.Title = states[0].Title;
        ...
    }
}

I'm currently not being able to acess the list as it is in a private method. What can I do?

Upvotes: 0

Views: 572

Answers (2)

Mikhail Troshchenko
Mikhail Troshchenko

Reputation: 51

Your states variable is created in a stack and exists only in onWorkflowActivated_Invoked event scope. As soon as method is finished, variable does not exist anymore. As a solution I would offer you to move this variable definition out of event scope. You can read more about it here: http://www.albahari.com/valuevsreftypes.aspx

Upvotes: 0

WasteD
WasteD

Reputation: 778

What if you just create an instance var?

 class YourClass {
    private List<State> states;
    private void onWorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
    {
         string json = @"[
         {
            'StateID': 1,
            'Title': 'State One'
         },
         {
            'StateID': 2,
            'Title': 'State Two'
         }
         ]";

         Json j = new Json();

         states = JsonConvert.DeserializeObject<List<State>>(json);
     }
}

Upvotes: 1

Related Questions