Shouldn't this work? (self referencing lambda function)

KevinNewmanKevinNewman USMember ✭✭

I have this code in my app:

EventHandler<TextChangedEventArgs> handler = (object sender, Android.Text.TextChangedEventArgs e) => {
    // ...
    element.TextChanged -= handler;
};
element.TextChanged += handler;

This was working before today's update, even though the code coloring showed the inner handler reference to be red. But since the update to Xamarin Studio 5 I'm getting a compile error.

Shouldn't this be working?

Posts

  • RolfBjarneKvingeRolfBjarneKvinge USXamarin Team Xamurai
    edited May 2014

    You're using handler before it's assigned (inside the lambda).

    Try this instead:

    EventHandler<TextChangedEventArgs> handler = null;
    handler = (object sender, Android.Text.TextChangedEventArgs e) => {
        // ...
        element.TextChanged -= handler;
    };
    element.TextChanged += handler;
    
  • KevinNewmanKevinNewman USMember ✭✭

    That makes sense. It used to work the way I had it until Xamarin Studio 5 - I guess it could be considered an error that was fixed?

Sign In or Register to comment.