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
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
Prashant JadhavPosted Jul 22, 2014, 3:11 AM
you are the only person who really intrested in others problem
and tend to solve it
i really appreciate
thank you very much
VulpesPosted Jul 21, 2014, 4:51 PM
Prashant JadhavPosted Jul 21, 2014, 2:27 PM
and events are use to overcome this problem.
VulpesPosted Jul 21, 2014, 8:54 AM
It's just a simple example to illustrate that you can do things using an ordinary delegate which would be regarded as undesirable in the publish/subscribe pattern. Those things simply aren't possible if you use an event instead because the code won't compile.
Prashant JadhavPosted Jul 21, 2014, 8:01 AM
i hope you will answer my question
as coding that you have given you have passed even publisher class to an method outside of main method as following
since we all create our application logic in main method and our application gets end when main method gets end my point is why would any programmer send event publisher object that has created to another method outside of main method which is the original application.why would someone create such bug in his own application.
VulpesPosted Jul 20, 2014, 4:23 PM
1. Get details of other subscribers.
2. Replace other subscribers by reassigning the delegate.
3. Clear all subscribers by setting the event to null.
Here's a simple example which illustrates these points:
If in the EventProvider class, you now change this line:
public EventHandler MyEvent;
to this:
public event EventHandler MyEvent;
then it simply won't compile and you'll be informed on 3 occasions that:
Prashant JadhavPosted Jul 20, 2014, 3:32 PM
thank you for replying
i didnt get you following statement
This restriction ensures that a subscriber to the event cannot find out anything about or interfere with other subscribers to the event which is the main reason for having them in the first place.
if you mean that we only register methods with event is advantage of event the what about delegate even with delegate we can only register method thats it what else can someone do
VulpesPosted Jul 20, 2014, 3:03 PM
This restriction ensures that a subscriber to the event cannot find out anything about or interfere with other subscribers to the event which is the main reason for having them in the first place.
So, whatever you can do with an event you'll also be able to do with an 'ordinary' delegate but the former is safer.