Returning value from lambda expression -- c#

I am trying to use the Unity Messaging System.

I have an interface

 public interface ICustomMessageTarget : IEventSystemHandler  
  {
        bool Message1(); 
  }

.... class with implementing interface that contains a method to execiute

public class CustomMessageTarget : MonoBehaviour, ICustomMessageTarget
     {
         public bool Message1()
         {
             // do something
             return true;
         }
     }

... and function "SomeFunction" which calls "ExecuteEvents.Execute" with the parameter "Message1"."ExecuteEvents" is a lambda expression

 public void SomeFuncion()
     {
         bool ReturnBool = ExecuteEvents.Execute<ICustomMessageTarget> 
         (SomeObject, null, (x, y) =>  x.Message1());
     }

I would like "Message 1" return bool value to "SomeFunction" Unfortunately "SomeFunction" above code will show an error.

How to do it that "Message 1" returns bool value to "SomeFunction"?

I got compilation error "An embedded statement may not be a declaration or labeled statement" error in "SomeFuncion"

1 answer

  • answered 2018-04-14 14:21 MaestroNate

    From first glance it looks like you are missing the closing parenthetical in the next to last line in your second example (at the end of the lambda expression):

    public void SomeFuncion()
         {
             bool ReturnBool = ExecuteEvents.Execute<ICustomMessageTarget> 
             (SomeObject, null, (x, y) =>  x.Message1());
         }
    

    Try that and let us know if that works!