T
T
TheTalion2016-10-18 16:57:56
C++ / C#
TheTalion, 2016-10-18 16:57:56

Implicit conversion from void to user method. What's my mistake?

Good afternoon, I ran into the problem of implicit type conversion, but I can’t understand why it occurs, because everything seems to be correct. Please explain what is my mistake or show the correct execution.

на C#
public int PlayerReady;

delegate void UI();
  class OnReady
  {
    public event UI ReadyEvent;
    public void OnReadyEvent()
    {
      ReadyEvent();
    }
  }

  class UserReady
  {
    bool uiReady;
    public UserReady(bool Ready)
    {
      this.Ready = Ready;

    }
    public bool Ready { set { uiReady = value; } get { return uiReady; } }
    public void OnReadyHandler(int PlayerReady)
    {
      WriteLine("событие вызвано!\n");
      EventPlus(ref PlayerReady);

    }
    void EventPlus (ref int PlayerReady) {
      PlayerReady++;
    }
  }
class Program
    {
        static void Main()
        {
  var evt = new OnReady();
  var UR = new UserReady(ready);
  evt.ReadyEvent += UR.OnReadyHandler(server.PlayerReadyToGame);//тут ошибка "Не удается неявно преобразовать тип void в Example.UI" 
}
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tex0, 2016-10-18
@TheTalion

evt.ReadyEvent += UR.OnReadyHandler(server.PlayerReadyToGame);

You hook the result of the execution of the UR.OnReadyHandler function to the event (and it returns this same void), but you need a handler (delegate object)
Something like this:
Apparently you did not understand the topic of delegates and events. Read MSDN.
And as mentioned above, the signature of the handler must match the signature of the delegate that defines the event.

I
Ivan, 2016-10-18
@Iv_and_S

<ClassOrObject>.<EventName> += <ClassWhoseMethodShouldRun>.<Method MatchedBy Signature>
You have a delegate signature delegate void UI();that is different from a method signature public void OnReadyHandler(int PlayerReady). The signature of the delegate must be void (int)in this case.

A
Artyom Karetnikov, 2016-11-28
@art_karetnikov

The magic is most likely that third-party comments first await the approval of the site owner. Go and see what you have in them. :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question