Events and delegates: help!
-
Hello! I've been programming in C# for about a month now with a book called Beginning Visual C#.NET (Wrox Press). So yesterday I got to the Events chapter and... I really need your help! In this book the author sais and explains that events are used with delegates. So you kind of use both... But I can't get it: why are they used with delegates? How do you put them together? I can undestand events like this (used from a thread nearby...) with no problems: private void AddButtons() { Button B1 = new Button(); B1.Text = "OK"; B1.Location = new System.Drawing.Point(8,8); B1.Click += new System.EventHandler(buttonB1Click); } private void buttonB1Click(object sender, System.EventArgs e) { MessageBox.Show("Hello"); } ------ I can understand delegates like this (example from my book): class Class1 { delegate double processDelegate(double param1, double param2); static double Multiply(double param1, double param2) { return param1 * param2; } static double Devide(double param1, double param2) { return param1 * param2; } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { processDelegate process; Console.WriteLine("Enter 2 numbers separated with a comma:"); string input = Console.ReadLine(); int commaPos = input.IndexOf(','); double param1 = Convert.ToDouble(input.Substring(0, commaPos)); double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1)); Console.WriteLine("M for multiply, D for devide:"); input = Console.ReadLine(); if (input == "M") process = new processDelegate(Multiply); else process = new processDelegate(Devide); Console.WriteLine("Result {0}", process(param1, param2)); } } } ----------------------- But I CANNOT undestand what do they have to do with each other? Why use them, when I can use events like in the example above? What do delegates do that helps with events? Please, explain thoroughly in a non-article dummy-undestandble way, as I have read a lot of articles on it today, but still it seems that in all of them it's a given... Please! Because I really need to move on, but I can't since I cannot undestand the topic! If you want, I can kindly 'reward' you with a Gmail account, no probs with that! But please help! Thanks!
-
Hello! I've been programming in C# for about a month now with a book called Beginning Visual C#.NET (Wrox Press). So yesterday I got to the Events chapter and... I really need your help! In this book the author sais and explains that events are used with delegates. So you kind of use both... But I can't get it: why are they used with delegates? How do you put them together? I can undestand events like this (used from a thread nearby...) with no problems: private void AddButtons() { Button B1 = new Button(); B1.Text = "OK"; B1.Location = new System.Drawing.Point(8,8); B1.Click += new System.EventHandler(buttonB1Click); } private void buttonB1Click(object sender, System.EventArgs e) { MessageBox.Show("Hello"); } ------ I can understand delegates like this (example from my book): class Class1 { delegate double processDelegate(double param1, double param2); static double Multiply(double param1, double param2) { return param1 * param2; } static double Devide(double param1, double param2) { return param1 * param2; } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { processDelegate process; Console.WriteLine("Enter 2 numbers separated with a comma:"); string input = Console.ReadLine(); int commaPos = input.IndexOf(','); double param1 = Convert.ToDouble(input.Substring(0, commaPos)); double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1)); Console.WriteLine("M for multiply, D for devide:"); input = Console.ReadLine(); if (input == "M") process = new processDelegate(Multiply); else process = new processDelegate(Devide); Console.WriteLine("Result {0}", process(param1, param2)); } } } ----------------------- But I CANNOT undestand what do they have to do with each other? Why use them, when I can use events like in the example above? What do delegates do that helps with events? Please, explain thoroughly in a non-article dummy-undestandble way, as I have read a lot of articles on it today, but still it seems that in all of them it's a given... Please! Because I really need to move on, but I can't since I cannot undestand the topic! If you want, I can kindly 'reward' you with a Gmail account, no probs with that! But please help! Thanks!
Here's a description of delegates and events: Events and Delegates on MSDN[^] Basically, by adding an event handler to an event you subscribe your listener to be informed when the particular event occurs. Your class is informed by the event source by calling your delegate function when the event occurs. For the event source class to be able to call your function it has to know what parameters an event handler function expects. That's why delegates have to be declared. Think of delegates as type safe function pointers. mav
-
Here's a description of delegates and events: Events and Delegates on MSDN[^] Basically, by adding an event handler to an event you subscribe your listener to be informed when the particular event occurs. Your class is informed by the event source by calling your delegate function when the event occurs. For the event source class to be able to call your function it has to know what parameters an event handler function expects. That's why delegates have to be declared. Think of delegates as type safe function pointers. mav
Damn... maybe I am just hopelessly stupid today (and yesterday) but I still can't get the hang of it... Everything seems to be perfectly clear from your explanation, but somehing is still missing. Sorry :( And C# is my first language, so I don't know C++ with function pointers... Thanks for the reply anyway. Need a Gmail account? Just send me a Email to gbelov@gmail.com and you'll have it! Anyone else to help a dumb hopeless comedian calling himself a C# programmer? :(
-
Damn... maybe I am just hopelessly stupid today (and yesterday) but I still can't get the hang of it... Everything seems to be perfectly clear from your explanation, but somehing is still missing. Sorry :( And C# is my first language, so I don't know C++ with function pointers... Thanks for the reply anyway. Need a Gmail account? Just send me a Email to gbelov@gmail.com and you'll have it! Anyone else to help a dumb hopeless comedian calling himself a C# programmer? :(
Ok, lets see if I can explain myseflt: Basically, an Event needs a delegate of the method its supposed to call when the event fires. In your example:
private void AddButtons() { Button B1 = new Button(); B1.Text = "OK"; B1.Location = new System.Drawing.Point(8,8); B1.Click += new System.EventHandler(buttonB1Click); } private void buttonB1Click(object sender, System.EventArgs e) { MessageBox.Show("Hello"); }
u are using a delegate although you might not be aware of it. The delegate type is: public delegate void EventHandler(object sender, System.EventArgs e); and the instance is pointing at buttonB1Click(object sender, System.EventArgs e) if u dont see it too clearly, use intellisense and create a new instance of the delegate: EventHandler handler=new EventHandler(.....); Anyway, u will see this more clearly when u try to create ur own Events for your own classes.