1

I want to create a method which returns new object and takes delegate as parameter. Delegate should operate with that object. I would like to not put that object as parameter and use object that returns my function. Is it possible to make this code running?

    public class ActionContext
    {
        public Action Action;
        public int Variable = 0;
    }

    public ActionContext Create(Action action)
    {
        return new ActionContext(){ Action = action };    
    }

    public void Test()
    {
        // I don't want provide ActionContext through delegate(ActionContext)
        ActionContext context = Create(delegate
        {
            //ERROR: Use of unassigned local variable 'context'
            context.Variable = 10;
        });

        context.Action.Invoke();
    }
Aleksei Zyrianov
  • 2,294
  • 1
  • 24
  • 32
Vlad Mysla
  • 1,181
  • 12
  • 15

3 Answers3

2

Change it to this:

public void Test()
{
    ActionContext context = null; // Set it to null beforehand
    context = Create(delegate
    {
        context.Variable = 10;
    });

    context.Action.Invoke();
}

And it compiles and works fine.

In your version of the code, the compiler tries to use (capture) the variable when it is still unassigned. But we know that the context variable is being assigned before the anonymous method is going to be called. So we can assign a temporary value to it so that the compiler doesn't complain.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
  • thank you, i though there is a way to implement it without initial declaration – Vlad Mysla May 09 '13 at 20:11
  • 1
    @Vlad You're welcome. You can mark the answer as accepted if it helped you solve your problem. Please read [FAQ]. BTW, welcome to StackOverflow :-) – Mohammad Dehghan May 10 '13 at 08:41
  • actually, i knew about this solution, but thought that its possible make this without initial definition. your answer can help people who is new C#, so i'll accept it to not confuse them. anyway if you will find something else - feel free to share your ideas. thank you! – Vlad Mysla May 26 '13 at 10:30
1
public class ActionContext
{
    public Action Action;
    public int Variable = 0;
    public delegate void Foo(ref int i);

    public ActionContext(ActionContext.Foo action)
    {
        Action = () => action(ref this.Variable);    
    }
}



public void Test()
{
    // I don't want provide ActionContext through delegate(ActionContext)
    ActionContext context = new ActionContext(
        (ref int variable) => variable = 10);

    context.Action.Invoke();
}
Aron
  • 15,464
  • 3
  • 31
  • 64
1
public class ActionContext
{
    public Action Action;
    public int Variable = 0;


   public Func<ActionContext> Create(Action action)
   {
        return (() => { Action = action; return this; });
   }


   public void Test()
   {
      // I don't want provide ActionContext through delegate(ActionContext)
      var context = Create(() => { Variable = 10; });
     context().Action.Invoke();
   }

}
valerysntx
  • 506
  • 3
  • 7