12

I have

class A
{
    B b;

    //call this Method when b.Button_click or b.someMethod is launched
    private void MyMethod()
    {            
    }

    ??
}

Class B
{
    //here i.e. a button is pressed and in Class A 
    //i want to call also MyMethod() in Class A after the button is pressed 
    private void Button_Click(object o, EventArgs s)
    {
         SomeMethod();
    }

    public void SomeMethod()
    {           
    }

    ??
}

Class A has a instance of Class B.

How can this be done?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Gobliins
  • 3,848
  • 16
  • 67
  • 122

3 Answers3

69

You'll need to declare a public event on class 'B' - and have class 'A' subscribe to it:

Something like this:

class B
{
    //A public event for listeners to subscribe to
    public event EventHandler SomethingHappened;

    private void Button_Click(object o, EventArgs s)
    {
        //Fire the event - notifying all subscribers
        if(SomethingHappened != null)
            SomethingHappened(this, null);
    }
....

class A
{
    //Where B is used - subscribe to it's public event
    public A()
    {
        B objectToSubscribeTo = new B();
        objectToSubscribeTo.SomethingHappened += HandleSomethingHappening;
    }

    public void HandleSomethingHappening(object sender, EventArgs e)
    {
        //Do something here
    }

....
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
  • 16
    Dunno why it's so hard to search a C# event example as clear and simple as this one to, just to communicate two classes? After hours searching finally... I wanna cry, thank you very much. – Konayuki May 29 '17 at 07:15
  • 4
    @Konayuki: Could not agree more. I've been looking for a simple example of this, and this is it. (at)DaveBish: good job - simple and effective. – Paul Feb 12 '18 at 11:38
9

You need three things (which is marked by comments in code):

  1. Declare event in class B
  2. Raise event in class B when something happened (in your case - Button_Click event handler executed). Keep in mind that you need to verify if there are any subscribers exists. Otherwise you will get NullReferenceException on raising event.
  3. Subscribe to event of class B. You need to have instance of class B, which even you want to subscribe (another option - static events, but those events will be raised by all instances of class B).

Code:

class A
{
    B b;

    public A(B b)
    {
        this.b = b;
        // subscribe to event
        b.SomethingHappened += MyMethod;
    }

    private void MyMethod() { }
}

class B
{
    // declare event
    public event Action SomethingHappened;

    private void Button_Click(object o, EventArgs s)
    {
         // raise event
         if (SomethingHappened != null)
             SomethingHappened();

         SomeMethod();
    }

    public void SomeMethod() { }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
-1

Have a look at rasing an event from Class B

Have a look at

Raising an Event

Handling and Raising Events

How to: Raise and Consume Events

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284