tags:

views:

774

answers:

3

I have class

//Create GroupFieldArgs as an EventArgs
    public class GroupFieldArgs : EventArgs
    {
        private string groupName = string.Empty;
        private int aggregateValue = 0;
        //Get the fieldName
        public string GroupName
        {
            set { groupName = value; }
            get { return groupName; }
        }
        //Get the aggregate value
        public int AggregateValue
        {
            set { aggregateValue = value; }
            get { return aggregateValue; }
        }
    }

I have another class that creates a event handler

public class Groupby
{
 public event EventHandler eh;
}

Finally I have Timer on my form that has Timer_TICK event. I want to pass GroupFieldArgs in Timer_TICK event. What is the best way to do it?

A: 

I am not sure what you are trying to achieve. The Timer.Tick event always returns plain EventArgs, because it is driven from within the .Net framework. Do you want to return GroupFieldArgs from the event you defined in Groupby class?

Grzenio
A: 

You could subclass the standard timer class and add a property where you can inject your custom event args. Your new class could handle the Elapsed event internally, and expose a new event which would carry your EventArgs.

namespace Test
{
class MyTimer : System.Timers.Timer
 {
 public EventArgs MyEventArgs { get; set; }
 public MyTimer()
  {
  this.Elapsed += new System.Timers.ElapsedEventHandler(MyTimer_Elapsed);
  }
 void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
  if (MyEventArgs != null)
   OnMyTimerElapsed(MyEventArgs);
  else
   OnMyTimerElapsed(e);
  }
 protected virtual void OnMyTimerElapsed(EventArgs ea)
  {
  if (MyTimerElapsed != null)
   {
   MyTimerElapsed(this, ea);
   }
  }
 public event EventHandler MyTimerElapsed;
 }
}
Tim Long
A: 

As Grzenio pointed out, it's always going to generate the same kind of event.

That said, you can create an object that "bounces" the event to its intended recipients...

public class GroupFieldArgSender
{
  public string GroupName  { get; set; }
  public int AggregateValue { get; set; }

  public void RebroadcastEventWithRequiredData(object sender, EventArgs e)
  {
    if (GroupFieldEvent != null)
    {
      GroupFieldEvent(sender, new GroupFieldEvent
        { GroupName = GroupName, AggregateValue = AggregateValue });
    }
  }

  public event EventHandler<GroupFieldEventArgs> GroupFieldEvent;
}

Then wire the timer tick event to the RebroadcastEventWithRequiredData method.