Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribe event to event in C#?

Tags:

c#

Is it possible in C# to connect one event to another so emitting first event will emit second one? The only way i can see is to create a stub function that will emit second event and connect the first event to this function. Compiler do not want to connect event to event or event to anonymouse function / lambda that calls another event:

class Ui { public event EventHandler OnClick; }
class Logic { public event EventHandler OnExit; }
var ui = new Ui();
var logic = new Logic();
ui.OnClick += logic.OnExit; // Not working.
ui.OnClick += ( a, b ) => logic.OnExit; // Not working either :(.

Maybe it's some decorator available or some black magic that allows to chain events without stub functions?

like image 430
grigoryvp Avatar asked Oct 29 '09 21:10

grigoryvp


People also ask

Which of the statements can be used to subscribe to an event?

To subscribe to events programmatically Use the addition assignment operator ( += ) to attach an event handler to the event.

What is EventArgs C#?

EventArgs is also the class you use when an event does not have any data associated with it. When you create an event that is only meant to notify other classes that something happened and does not need to pass any data, include the EventArgs class as the second parameter in the delegate. You can pass the EventArgs.

How do you raise an event in C#?

Raising an events is a simple step. First you check the event agaist a null value to ensure that the caller has registered with the event, and then you fire the event by specifying the event by name as well as any required parameters as defined by the associated delegate. MyEvent(message);

What is the difference between event and delegate in C#?

An event is declared using the event keyword. Delegate is a function pointer. It holds the reference of one or more methods at runtime. Delegate is independent and not dependent on events.


2 Answers

You cannot do this, because you generally cannot do anything to an event from outside the object which owns it except for adding and removing handlers. In particular, you cannot list the existing registered handlers, and you cannot raise it. In your case, "copying" the event is essentially the same thing in disguise, and would allow you to circumvent this restriction; therefore, it's not allowed.

See this recent answer of mine for a more in-depth explanation of why things are the way they are - I just don't feel like retyping it all here.

For your particular case, if you own both classes, the workaround is to make them cooperate specifically - make Ui be aware of the associated Logic instance, and add event handlers to Logic.OnClick in Ui.OnClick.add implementation. Of course, this introduces coupling; you can reduce it to some extent by using more generic interfaces, but you can't get rid of it entirely.

As a side note, OnClick is not a good name for a .NET event. Common naming guide says that it should be simply Click (and OnClick should be the name of a protected virtual method that raises it).

like image 102
Pavel Minaev Avatar answered Sep 28 '22 08:09

Pavel Minaev


You could do this by hiding the underlying event for Logic and then controlling calls to Add/Remove which require a UI parameter.

public class UI {
  public EventHandler OnClick;
}
public class Logic {
  private event EventHandler _onExit;
  public void AddOnExit(UI ui, EventHandler e) {
    ui.OnClick += e;
    _onExit += e;
  }
  public void RemoveOnExit(UI ui, EventHandler e) {
    ui.OnClick -= e;
    _onExit -= e;
  }
}
like image 24
JaredPar Avatar answered Sep 28 '22 09:09

JaredPar