Prashant Jadhav

Prashant Jadhav

  • NA
  • 38
  • 13.6k

events and delegate

Jul 20 2014 2:41 PM
hi guys my question is is about event and delegates
as defination of event events are type of delegates it means event also behave as function pointer.
so why do we need event to point to event handler function we can create just a delegate and make it regester with the
event handler fuction.
even i tried this and i have not received any error heres the coding which i tried
plz go through it and reply me whether i made any mistake.
using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached +=c_ThresholdReached;

Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
}

static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Console.ReadLine();
}
}

class Counter
{
private int threshold;
private int total;

public Counter(int passedThreshold)
{
threshold = passedThreshold;
}

public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}

protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{

if (ThresholdReached != null)
{
ThresholdReached(this, e);
}
}
public eventhand ThresholdReached;
public delegate void eventhand(object a,ThresholdReachedEventArgs e);
}

public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}


and theres another code we can get on msdn which use even to make subscriber class to register event handler function.

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached += c_ThresholdReached;

Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
}

static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}

class Counter
{
private int threshold;
private int total;

public Counter(int passedThreshold)
{
threshold = passedThreshold;
}

public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}

protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}

public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}

public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}

Answers (8)