c# - Adding one event handler to another -


I have a class that wraps another class and exposes many incidents from that class of wrapping. (For example it changes).

I used the following code:

  Public event eventHandler Annievent; Public other class inner {get {/ * ... * /} set {// ... if (value! = Null) value.AnEvent + = AnEvent; // ...}}  

However, incidents were raised inconsistently.

What's wrong with this code?

- - Here are two problems ...

First: Both In cases, you are raising the external event with the wrong sender Anyone subscribing to an event on the outside class will expect that those classes will be picked up from the sender of that external class.

This is especially applicable to the wifier control or compulsive list, where sender is used to identify the object that would share a handler. Are there.

It should be something instead:

  zero Inner_AnEvent (object sender, EventArgs e) {var handler = AnEvent; If (handler! = Null) handler (this, e); } The second (more modest) issue is that you are currently taking an event on the internal category, even if there is no member of the outer classroom. You can fix it with a little more custom handling ...  
  private event handler anEvent; Public Event EventHandler ANEEvent {add {// note: synchronized boole not = an avent == empty; AnEvent + = value; If (first & amp; annewand! = Null & amp; internal! = Null) {inner.SomeEvent + = Inner_AnEvent; }} Remove {// Note: The unbalanced bull was value = an avent! = Null; AnEvent - value =; If (was the value & amp; enevent == zero and internal! = Tap) {inner.SomeEvent - = Inner_AnEvent; }}  

(and similar codes are found in Inner / only subscribe if we have listeners ...

  if (value = = Null & amp; anEvent = null) value.AnEvent + = Inner_AnEvent;;  

This may be a bigger saver if you have lots of examples of external class, But rarely use.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -