-3

I want to give an out parameter on event but it doesn't work

like this

error :Cannot use parameter out in anonyme method or lambda expression

public void CallMyMethod()
{
  //{...  code here`removed...just for initialize object}

  int? count = null;
  MyMethod(myObject,count);
}
public static void MyMethod(AnObject myObject,out int?count)
{
    //{... code removed...}
    IEnumerable<AnAnotherObject> objects = myObject.GetAllObjects();//... get objects
    count = (count == null) ? objects.Count() : count;
    MyPopup popup = CreateMypopup();
    popup.Show();
    popup.OnPopupClosed += (o, e) =>//RoutedEventHandler
    {
        if (--count <= 0)
        {
            Finished();//method to finish the reccursive method;
        }
        else
        {
            MyMethod(myObject, out count);
        }
    };
}
Andry
  • 13
  • 3

1 Answers1

0

The standard way to pass parameters to event handler is to define your own class

public class MyObjectArgs: EventArgs
{
    MyObject myObj {get;set}
    int? Count {get; set;}
}

and then pass the same instance of this class down the recursive calls

public void CallMyMethod()
{
  //{...  code here`removed...just for initialize object}
  MyObjectArgs args = new MyObjectArgs();
  args.Count = null;
  args.myOby = myObject;
  MyMethod(args);
}

public static void MyMethod(MyObjectArgs args)
{
    //{... code removed...}
    IEnumerable<AnAnotherObject> objects = args.myObj.GetAllObjects();//... get objects
    args.Count = (args.Count == null) ? objects.Count() : args.Count;
    MyPopup popup = CreateMypopup();
    popup.Show();
    popup.OnPopupClosed += (o, e) =>//RoutedEventHandler
    {
        if (--args.Count <= 0)
        {
            Finished();//method to finish the reccursive method;
        }
        else
        {
            MyMethod(args);
        }
    };
}

Also the line where you count the objects seems to be incorrect, perhaps you want this ?

args.Count = (args.Count == null) ? objects.Count() : args.Count + objects.Count();
Steve
  • 213,761
  • 22
  • 232
  • 286