title: Adding an action to event handler arguments c#
body: <p>I have a problem with some code I need to refactor. Right now it uses lambdas as event handlers, but they are not removed properly. From what I have read, this is not even possible?  Anyway I would like to rewrite it to use a delegate instead of an anonymous function, and now my problem is that right now it takes an action as parameter, and I can't seem to figure out how to pass the action on to my new delegate. This is the code:</p>
  
  <pre><code>void RetrieveData(
              int pointId,
              int? chartCollectionId,
              Action action)
          {
              if (pointId &lt;= 0)
                  throw new ArgumentException("PointId not valid");
  
              LastPointId = NextPointId;
              NextPointId = pointId;
  
              Clear();
  
              _csr = new CustomerServiceRepository();
  
              _csr.ServiceClient.GetChartDataCompleted += (se, ea) =&gt;
                                                              {
                                                                  _cachedCharts = ea.Result;
                                                                  ChartDataRetrieved(ea.Result);
                                                                  if (action != null)
                                                                      action.Invoke();
                                                                  _csr = null;
                                                              };
              _csr.ServiceClient.GetChartDataAsync(
                  Settings.Current.Customer.CustomerName,
                  pointId,
                  chartCollectionId);
  
              _csr.ServiceClient.GetChartDataCompleted -= (se, ea) =&gt; //remove after usage
                                                              {
                                                                  _cachedCharts = ea.Result;
                                                                  ChartDataRetrieved(ea.Result);
                                                                  if (action != null)
                                                                      action.Invoke();
                                                                  _csr = null;
                                                              };
          }
  </code></pre>
  
  <p>I was thinking that maybe I could create the following:</p>
  
  <pre><code>public class extendedEventArgs : GetChartDataCompletedEventArgs
          {
              Action foo { get; set; }
          }
  
          void tang(object sender, extendedEventArgs e)
          {
              _cachedCharts = e.Result;
              ChartDataRetrieved(e.Result);
              if (action != null)
                  action.Invoke();
              _csr = null;
          }
  </code></pre>
  
  <p>And the pass the action as a parameter in the extended event args, but when I try to use it like this</p>
  
  <pre><code>_csr.ServiceClient.GetChartDataCompleted += new EventHandler&lt;extendedEventHandler&gt;(tang);
  </code></pre>
  
  <p>It gives an error:</p>
  
  <pre><code>Cannot implicitly convert type  System.EventHandler&lt;Conwx.Net.Client.CustomerClient.Controls.ChartControls.ChartListForecast.extendedEventArgs&gt;'  to  System.EventHandler&lt;Conwx.Net.Client.Framework.CustomerServiceReference.GetChartDataCompletedEventArgs&gt;'
  </code></pre>
  
  <p>What am I doing wrong here? Alternative solutions are also welcome.</p>
  
  <p>.K</p>