Loading ...

Delegates and events

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Delegates and events

Delegates and events

Posts under the topic: Delegates and events

Posted: 10/21/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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();
        }
    }
}



Posted: 10/21/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

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();
        }



tags C#, Delegates

Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Thank you Sir:)

 


Posted: 10/22/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

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.


Page 1 of 1 (4 items)