Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why not simply return member and subscribe to it's event?</p> <pre><code>public IMember MemberForKey(string key) // return IMember { if (!members.ContainsKey(key)) throw new Exception(); return members[key]; } </code></pre> <p>And then subscribe:</p> <pre><code>Container c = new Container(); c.CreateEventForKey("a"); c.MemberForKey("a").AnEvent += some_function; c.OnEventForKey("a", "b"); </code></pre> <p>But you have public <code>OnEvent</code> method in <code>Member</code> class. In order to forbid raising events by client, you can create interface which will show only event. Just implement this interface by <code>Member</code> class:</p> <pre><code>public interface IMember { event Action&lt;string&gt; AnEvent; } </code></pre> <p>And yes, you cannot return event, because actually event is not object, it is set of two methods <code>add</code> and <code>remove</code>, which add and remove delegates to inner field of delegate type. Here is how your event looks like:</p> <pre><code> private Action&lt;string&gt; _action; // field of delegate type public event Action&lt;string&gt; AnEvent { add { _action += value; } remove { _action -= value; } } </code></pre> <p>Purpose of event is to provide only two operations for clients - adding and removing handlers. Delegate itself is hidden to clients. You can make it public:</p> <pre><code>public Action&lt;string&gt; _action; </code></pre> <p>But in this case any client can invoke it.</p> <p>UPDATE: if you want to go with Subscribe/Remove syntax, then just use dictionary with handlers:</p> <pre><code>public class Container { private Dictionary&lt;string, Action&lt;string&gt;&gt; handlers = new Dictionary&lt;string, Action&lt;string&gt;&gt;(); public void CreateEventForKey(string key) { // with empty handler added you can avoid null check handlers.Add(key, (value) =&gt; { }); } public void OnEventForKey(string key, string value) { if (!handlers.ContainsKey(key)) throw new Exception(); handlers[key](value); } public void Subscribe(string key, Action&lt;string&gt; handler) { if (!handlers.ContainsKey(key)) throw new Exception(); handlers[key] += handler; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload