Use of event handling

Z

Zach

I have a question re event handling. I have knocked up two examples,
code #one and code #two (below). Both do the same thing. I can
understand using event handling with respect to changed conditions of
controls, or subscribing to an event in a different class, but what use
would code #two be in other situations? Is it"l'art pour l'art"?


Code #one

using System;

namespace alternative
{
class Program
{
static void Main(string[] args)
{

int current = 0;
int previous = 0;

while(true)
{
System.DateTime dt = System.DateTime.Now;
current = dt.Second;
if(current %5 == 0 && current != previous)
Console.WriteLine("event raised {0}", current);
previous = current;
}
}
}
}

Code #two

using System;

namespace Event
{
public delegate void MyEventHandler(object o, MyEventArgs e);

public class MyEventArgs : EventArgs
{
public readonly int Second;
public MyEventArgs(int second)
{
this.Second = second;
}
}

public class Subscriber
{
public void show(object o, MyEventArgs e)
{
Console.WriteLine("Event raised by {0}", e.Second);
}
}

class Program
{
public static event MyEventHandler EventSecond;
public static void Main(string[] args)
{
int previous = 0;
System.DateTime current = System.DateTime.Now;
Subscriber Li = new Subscriber();
EventSecond += new MyEventHandler(Li.show);
while (true)
{
System.DateTime dt = System.DateTime.Now;
int sec = dt.Second;

MyEventArgs e = new MyEventArgs(sec);
if (sec%5 == 0 && sec != previous)
EventSecond(new Subscriber(), e);
previous = sec;
}
}
}
}
 
Z

Zach

Of course, this is not true for other code examples using events.
<snips>
Actually I had looked at different examples on the Internet, derived a
template from those examples and knocked up the code I posted on the
bases if that template. If you should have a reference to other (and
better) examples using events I would be interested.
Zach.
 
Z

Zach

Practically any of the examples found on MSDN or in this newsgroup would
be better than what you posted.

I'm curious what "example on the Internet" you were following that
actually used an event in the way that the code you posted here does.
Post a link.

But suffice to say, the code you posted does not bear much resemblance
to any real-world use of an event. It demonstrates the mechanism well
enough, but it doesn't offer any insight regarding coding scenarios
where an event actually adds value. The examples you'd find on MSDN as
well as here in this newsgroup do.

Pete

This (below) is one of the code examples I found on the Internet. I no
longer have the Internet source location. It is the example I rebuilt
using my template to illustrate the mechanism. My code also clearly
shows where branches could be added to increase the complexity of the
purpose served. I included code illustrating how the same function could
be achieved much easier, more branches could be added to the easier code
for more complexity as well. I think my original query clearly
communicates my question: why go the events route, when that route seems
unnecessarily complicated. You say other examples would answer my query,
so perhaps I might have a look at one of your better examples.

using System;
using System.Threading;

public class DataHolder : EventArgs
{
public readonly int second;
public DataHolder( int second)
{
this.second = second;
}
}

//This is the publishing class
public class Clock
{
private int second;
public delegate void EventHandler (object clock, DataHolder NewTime);
public event EventHandler OnSecondChange;
public void Run()
{
while(true)
{
System.DateTime dt = System.DateTime.Now;
if (dt.Second != second)
{
DataHolder NewTime = new DataHolder(dt.Second);
OnSecondChange(this,NewTime);
}
second = dt.Second;
}
}
}

//This is the subscribing class
public class DisplayClock
{
public void Subscribe(Clock theClock)
{
theClock.OnSecondChange += new Clock.EventHandler(TimeHasChanged);
}
public void TimeHasChanged(object theClock, DataHolder ti)
{
Console.WriteLine("Current Time:{0}", ti.second.ToString());
}
}

class Tester
{
[STAThread]
static void Main()
{
Clock theClock = new Clock();
new DisplayClock().Subscribe(theClock);
theClock.Run();
}
}
 
Z

Zach

On 12/24/11 2:06 AM, Zach wrote:

... rather than making a
futile attempt to defend a poor code example as useful for understanding
events better (an odd claim from someone who doesn't yet actually
understand events), just ask direct questions related to your lack of
understanding about events and why we use them.

Pete

You said you had better examples demonstrating what event handling is
about than the code I originally posted, so I kindly asked you for such
an example. I would have thought that to be a direct question.

Zach
 
Z

Zach

Beyond that, you even have admitted to having seen a better example and
posted the code for that example here.

(1.) I have posted extra code upon your request concerning other code I
had seen. I have not said that code was better.

(2.) I have posted two bits of code one about events handling, the other
doing the same thing with much less code lines.

(3.) The code I posted has the key elements I have seen in examples of
event handling. I have no questions to ask about that code.

********************************
! The question I asked was about the
! usefulness / applicability of event
! handling other than the obvious use
! relating to changed conditions
! of e.g. text boxes, list views,
! subscribing to events in other classes
! than the one in focus etc. However,
! you suggest I should be asking
! other/different questions.
********************************
 
K

Kris Sheglova

Zach,

Events are just a way for once class to notify others that something
has happened (e.g. a MailClient class might want to notify a Form that
mail has been received so it can update the UI and display a
notifcation in the system tray). It is the .Net way of implementing
the observer pattern.

For more detailed information you might want to look at the MSDN
tutorial:

http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Regards,

Kris Sheglova
 
Z

Zach

Zach,

Events are just a way for once class to notify others that something
has happened (e.g. a MailClient class might want to notify a Form that
mail has been received so it can update the UI and display a
notifcation in the system tray). It is the .Net way of implementing
the observer pattern.

For more detailed information you might want to look at the MSDN
tutorial:

http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

Regards,

Kris Sheglova
Thank you for your response to my question. I must say that in the past
I have employed to my advantage that one class can subscribe to an event
in another class e.g. on closing. I have downloaded your example an will
have a good look at it, and add it to my examples file.

Regards,
Zach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top