For you to add a delegate instance to ClassA from ClassB requires the instance of ClassB to have a
reference to an instance of ClassA.
And, the
signatures (parameters) must march.
A typical example:
public FormA AInstance;
public FormB BInstance;
private void MainForm_Load(object sender, EventArgs e)
{
AInstance = new FormA();
BInstance = new FormB();
}
public class FormA: Form
{
public Action<string> OnButtonClick;
private void btn_click(object sender, EventArgs e)
{
if (OnButtonClick != null) OnButtonClick("some text");
}
}
public class FormB: Form
{
public void SetClassAClickHandler(FormA ainstance)
{
ainstance.OnButtonClick += OnButtonClick;
}
private void OnButtonClick(string text)
{
}
}
Npte: the use of 'Action in ClassA is a recent C# language alternative syntax for writing a delegate that takes arguments, but does not return a value: [
^]