L
L
LiptonOlolo2016-06-02 12:05:50
C++ / C#
LiptonOlolo, 2016-06-02 12:05:50

How to bind event across multiple classes?

Good afternoon!
Faced a huge problem for me, tk. I’ve never dealt with events before, but right now I just need to use them :(
I’ll write the question itself like this: there are 1 dll, 2 classes, you need to bind an Event that will be signed in the main console application, from class 1 event is called which is in class 2 "Maybe I wrote it wrong. I just don't understand a bit about event.
I did this (next), but when I subscribe from the main project, there is nothing.

public class Test
    {
        public void Add(string text)
        {
            OnMessage(text);
        }
        public event Action<string> OnMessage;
    }

I call from class 1:
Test t = new Test();
t.Add(StringResponse);

So I subscribe in the main console application:
Test t = new Test();
t.OnMessage += tcp_OnMessage;

And nothing, absolutely nothing happens, please explain what I'm doing wrong :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LiptonOlolo, 2016-06-02
@LiptonOlolo

Understood.

#
#algooptimize #bottize, 2016-06-02
@user004

In any case, the subscription must be before abandoned messages, otherwise they will be lost, in this case there will be an exception in general, because there is no check for null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question