Events handling asking for comment

Z

Zach

Re the example I code below.
I have tried to make a very consise example
of events / eventshandling;
would you please comment?

(Don't tell me to read a book.)

Thanks,
Zach.

using System;

namespace EventsHandlingTest
{
public class TestClass
{
public class MakeEventArgs:EventArgs
{
public readonly int counter;
public MakeEventArgs(int counter)
{
this.counter = counter;
}
public override string ToString()
{
return counter.ToString ();
}

}
public class Publish
{
public delegate void EventHandler(object action, MakeEventArgs dh);
public event EventHandler onLimit;
int limit = 100;
int number = 0;

public void generateEvents()
{
while(true)
{
if(number++ > limit-2)
{
MakeEventArgs dh = new MakeEventArgs(number);
onLimit(this,dh);
number = 0;
}
}
}
}

public class SubscribingClass
{
public void subscribe(Publish publish)
{
publish.onLimit += new
EventsHandlingTest.TestClass.Publish.EventHandler(publish_onLimit);
}

public void publish_onLimit(object publish, MakeEventArgs dh)
{
Console.WriteLine(dh.ToString());
}

}

static void Main()
{
Publish publish = new Publish();
new SubscribingClass().subscribe(publish);
publish.generateEvents();
}
}
}
 
Z

Zach

public readonly int counter;

This should be private.

Since no comment is forthcoming I will assume
the example to be correct (apart from the above).

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