Although, this isn't your question, I'm going to throw this in there. You can check to see if anyone is listening to the event by "null-checking" it. This ensures that it's not going to execute the method (and subsequently throw a NullReferenceException).
public void Calling()
{
if (Message != null)
Message("Hello World"); // Why are you ignoring the return value?
}
As for your actual question, something has to have a reference to an object of type A
(as defined in your example.
public class B
{
A MyA;
public B()
{
MyA = new A();
}
}
Then it has to subscribe to the event of Message.
public class B()
{
A MyA;
public B()
{
MyA = new A();
MyA.Message += MessageHandler;
}
public string MessageHandler(string s)
{
// Do other code here and ensure you're returning a string as defined in your Message event.
return s;
}
}
Note that the MessageHandler
can be named anything. I just used that for clarity's sake. It can also be anonymous methods or lambdas rather than a named method.