Posted: 10/21/2010
I have created a very simple dummy program to understand Delegates and events. In my below program I am simple calling a method.When I call a method, five methods are automatically calll with the help of Delegates and events.
Kindly take a look on my program and do let me know where I am wrong or right as I am using first time Delegates and events.
using System; namespace ConsoleApplication1 { public delegate void MyFirstDelegate(); class Test { public event MyFirstDelegate myFirstDelegate; public void Call() { Console.WriteLine("Welcome in Delegate world.."); if (myFirstDelegate != null) { myFirstDelegate(); } } } class AttachedFunction { public void firstAttachMethod() { Console.WriteLine("ONE..."); } public void SecondAttachMethod() { Console.WriteLine("TWO..."); } public void thirdAttachMethod() { Console.WriteLine("THREE..."); } public void fourthAttachMethod() { Console.WriteLine("FOUR..."); } public void fifthAttachMethod() { Console.WriteLine("FIVE..."); } } class MyMain { public static void Main() { Test test = new Test(); AttachedFunction attachedFunction = new AttachedFunction(); test.myFirstDelegate += new MyFirstDelegate(attachedFunction.firstAttachMethod); test.myFirstDelegate += new MyFirstDelegate(attachedFunction.SecondAttachMethod); test.myFirstDelegate += new MyFirstDelegate(attachedFunction.thirdAttachMethod); test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fourthAttachMethod); test.myFirstDelegate += new MyFirstDelegate(attachedFunction.fifthAttachMethod); test.Call(); Console.ReadLine(); } } }
You can shorten your Main() as :
public static void Main() { var attachedFunction = new AttachedFunction(); var test = new Test(); test.myFirstDelegate += attachedFunction.firstAttachMethod; test.myFirstDelegate += attachedFunction.SecondAttachMethod; test.myFirstDelegate += attachedFunction.thirdAttachMethod; test.myFirstDelegate += attachedFunction.fourthAttachMethod; test.myFirstDelegate += attachedFunction.fifthAttachMethod; test.Call(); Console.ReadLine(); }
Posted: 10/22/2010
Thank you Sir:)
Raghav Sir I have implemented your code and created a new dummy project.There are three buttons. I have made an event and bind it for buttons.
Kindly take a look.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DelegatesAndEvents.aspx.cs" Inherits="WebProjects.DelegatesAndEvents" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btn1" runat="server" onclick="btn1_Click" Text="ONE" /><br /> <asp:Button ID="btn2" runat="server" onclick="btn2_Click" Text="TWO" /><br /> <asp:Button ID="btn3" runat="server" onclick="btn3_Click" Text="THREE" /><br /> <asp:Label ID="lblText" runat="server"></asp:Label> </div> </form> </body> </html>
using System; namespace WebProjects { public partial class DelegatesAndEvents : System.Web.UI.Page { public delegate void MyDelegate(string val); public event MyDelegate myEvent; protected void Page_Load(object sender, EventArgs e) { myEvent += SetLabelValue; } public void SetLabelValue(string val) { lblText.Text = val; } protected void btn1_Click(object sender, EventArgs e) { if(myEvent!=null) { string value = "button one is clicked."; myEvent(value); } } protected void btn2_Click(object sender, EventArgs e) { if (myEvent != null) { string value = "button two is clicked."; myEvent(value); } } protected void btn3_Click(object sender, EventArgs e) { if (myEvent != null) { string value = "button three is clicked."; myEvent(value); } } } }
PLEASE Do let me know your feedback or comment.