Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify when event from another class is triggered [duplicate]

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?

like image 597
Gobliins Avatar asked Dec 13 '12 09:12

Gobliins


People also ask

What is EventArgs?

The EventArgs class is the base type for all event data classes. EventArgs is also the class you use when an event doesn't have any data associated with it.

How to subscribe method to event C#?

To subscribe to events by using the Visual Studio IDEOn top of the Properties window, click the Events icon. Double-click the event that you want to create, for example the Load event. Visual C# creates an empty event handler method and adds it to your code. Alternatively you can add the code manually in Code view.

How do event handlers work C#?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.

What are events in C# with example?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.


2 Answers

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
    }

....
like image 131
Dave Bish Avatar answered Oct 12 '22 04:10

Dave Bish


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() { }
}
like image 35
Sergey Berezovskiy Avatar answered Oct 12 '22 04:10

Sergey Berezovskiy