 |
|
 |
e.g. C# noobs like me. Coming from loads of vb.net I found many on-line examples of C# events confusing. This was a eureka moment, cheers Todd. Give that man a beer.
|
|
|
|
 |
|
|
 |
|
 |
I am new to .Net. Kindly explain the above example. Thank you.
|
|
|
|
 |
|
 |
Did exactly what I was looking for, took me about 5 minutes to use, fantastic work Todd, cheers!
|
|
|
|
 |
|
 |
I like the simple example, thanks! The only thing, it would be better if you wrote the lines like this
public EventArgs e = null; public delegate void TickHandler(Metronome m, EventArgs e); public event TickHandler Tick; instead of public event TickHandler Tick; public EventArgs e = null; public delegate void TickHandler(Metronome m, EventArgs e);
...so people who are reading will not have to say "Where's TickHandler defined? or Where's e defined?" and not have to look for it, if those were already written first. The logic may work either way, but this is just for ease of reading (I understand they don't have to be written this way but it would sure help especially to beginners like me)
Thanks!
|
|
|
|
 |
|
 |
can u share some material for understanding Events and Delegates better.....
I m new to programing ...... my email id is pratapdessai@gmail.com
Also any help in understanding OOPS concept would be very helpful
Thanks in Advance.
Pratap Dattaram Dessai
|
|
|
|
 |
|
|
 |
|
 |
As a newbie (probably too newbie), I don't understand how to look at this article/code and create a complete Visual Studio Solution which has the "metronome" class definition and a separate WinForm demo project using the class. I would appreciate help in making this transformation. Thank you for considering my question.
|
|
|
|
 |
|
 |
So the code that I posted was for a console application. Thats why I have two classes, the metronome and the test class with the static main method. For use in a winforms application, Microsoft writes the main method inside the program.cs class for you, so dont worry about the test class at all. Just add a new class file to the solution, and paste the metronome class code inside of the new file you just created.
For a winforms application the application flow starts from the form code, either in the constructor or maybe inside the Form_Loaded event. Lets choose the form constructor. If you open the form file and press f7 you'll be taken to the code behind file. It will look something like this (I don't have VS with me right now)
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
// add this code
Metronome metronome = new Metronome();
metronome.tick += Metronome_Ticked;
}
// add this method
void Metronome_Ticked(object sender, EventArgs e)
{
MessageBox.Show("TICK");
}
}
|
|
|
|
 |
|
 |
Thank you very much. That worked!
|
|
|
|
 |
|
 |
I have a main form that loads user controls. The main form needs to listen for events on the user controls. How can the examples you have be adapted for this scenario? I haven't found articles that deal with this explicitly. Thanks...
|
|
|
|
 |
|
 |
My example is for using events with classes (or controls) that you write yourself. For classes (or controls) that Microsoft has written for you, all that you need to do is subscribe to the event that the class publishes. If you want your main form (I assume a c# winforms Form control) to subscribe to one of its child controls events, like a button click event, then you just need a line of code to subscribe to the event and a method to receive the event. If you're using visual studio, it will create a method for you when you subscribe to the event.
Button1.Clicked += (just keep hitting tab here and you'll get something like...)
Button1.Clicked += new EventHandler(Button1_Clicked);
void Button1_Clicked(object sender, EventArgs e)
{
}
you actually don't need the "new EventHandler" part, you can actually write
Button1.Clicked += Button1_Clicked;
The VS designer writes these lines of code for you in that Form.Designer.cs class
Hope this comes close to answering your question
|
|
|
|
 |
|
 |
if (Tick != null)
{
Tick(this, e);
}
What will go wrong if I just raise it without checking?
Tick(this, e);
|
|
|
|
 |
|
 |
It will throw an exception, probably a null reference exception. Its a design decision to answer the question "what if no one subscribes to my event?" does the line execute silently? Someone like Eric Lippert decided not, that events will be nullable and will be null until they are subscribed to.
A good programmer would point out the interesting threading issue here too. I can check if an event is null, and enter the if block (at the time it is not null) and then in between me checking for null and calling the event, it turns to null. There are patterns on how to avoid this, I think Eric Lippert blogged on one of them.
|
|
|
|
 |
|
 |
Perfectly understandable. I am coming from VB.NET and until I found your article, events in C# looked like a total nightmare. As you say, there are too many convoluted examples posted in some places/books etc.
Cheers
|
|
|
|
 |
|
 |
You saved me 2 days of searching!
Finally after reading your article, my events work! Now I can fall asleep, at last )
Thanks for the article
|
|
|
|
 |
|
 |
Thank you for the simple article. After reading tons of articles and looking at your code, I got it. Sometimes the simples code speaks the most!
Hogan
|
|
|
|
 |
|
 |
How is this simplified?
It's an academic example that introduces a Listener class that is in-cohesive in that it also interfaces with the user. A pedantic comment maybe, but HeardIt will have to communicate with another class (probably a Form class) in a real-world application, negating the need for the Listener class.
What's wrong with all the other examples that have the event handler in the main user interface class?
PeterRitchie.com
|
|
|
|
 |
|
 |
public class Sender
{
public event SenderEventHndl send;
public delegate void SenderEventHndl();
public void Start()
{
while (true)
{
System.Threading.Thread.Sleep(3000);
if (send != null)
{
send();
}
}
}
static void Main(string[] args)
{
Receiver r = new Receiver();
Sender s = new Sender();
r.Subcribe( s );
s.Start();
}
};
public class Receiver
{
public void Subcribe( Sender s )
{
s.send += new Sender.SenderEventHndl( printIt );
}
private void printIt()
{
System.Console.WriteLine( "Heard it" );
}
};
|
|
|
|
 |
|
 |
Thats what I came up with also. EventArgs is never used in the authors example... Also... even simpler (2 classes and a function) using System; namespace wildert { public class Metronome { public delegate void TickHandler(); public event TickHandler Tick; public void Start() { while (true) { System.Threading.Thread.Sleep(3000); if (Tick != null) Tick(); } } } class Test { static void Main() { Metronome m = new Metronome(); m.Tick += new Metronome.TickHandler(HeardIt); m.Start(); } private static void HeardIt() { System.Console.WriteLine("HEARD IT"); } } }
|
|
|
|
 |
|
 |
I actually was not that well versed in C# when i wrote this article, but was proud I figured out how events worked.
Wildert
|
|
|
|
 |
|
 |
I have seen very few (if any?) examples this simple, and I thank you for posting the article.
|
|
|
|
 |
|
 |
Thanks for this dude, you just made it easy for me
Regards,
-Frank (www.franksemails.com)
|
|
|
|
 |
|
 |
I didn't know about EventArg.Empty, thats a good one. The only thing is that if I remember right, System.Eventhandler gets pissed if you send anything but EventArg (or EventArg.Empty I guess). So if you have your own event arguements to pass, like in the second example, I don't think system.eventargs works, but i'm just pulling from memory here, blowing my work day.
|
|
|
|
 |
|
 |
If you're going to write your own class to hold the event arguments, just derive from System.EventArgs. Then you can pass your custom class to standard event handlers.
|
|
|
|
 |