Click here to Skip to main content
15,883,531 members
Articles / Programming Languages / C#

Delegates and their role in Events in C# .NET

Rate me:
Please Sign up or sign in to vote.
3.95/5 (11 votes)
13 May 2009CPOL3 min read 40.9K   574   30  
A simple delegates example.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DelegateIssue
{
    public partial class Form1 : Form
    {
        MyData _data;
        Consumer _consumer1, _consumer2;
        public Form1()
        {
            InitializeComponent();
            _data = new MyData();
            _consumer1 = new Consumer(_data,"Consumer1");
            _consumer2 = new Consumer(_data,"Consumer2");            
        }

        private void operateButton_Click(object sender, EventArgs e)
        {
            if(_data.Operate!= null)
            _data.Operate();
        }

        

        private void wire1Button_Click(object sender, EventArgs e)
        {
            issueTextBox.Text = "";
            _consumer1.WireDelegate();
            consumer1TextBox.Text = "Consumer1 is wired for " + _consumer1.WiringCount + " time(s)";            
        }

        private void wireButton2_Click(object sender, EventArgs e)
        {
            issueTextBox.Text = "";
            _consumer2.WireDelegate();
            consumer2TextBox.Text = "Consumer2 is wired for " + _consumer2.WiringCount + " time(s)";           
        }

        private void unwire1Button_Click(object sender, EventArgs e)
        {
            _consumer1.UnWireDelegate();
            consumer1TextBox.Text = "Consumer1 is wired for " + _consumer1.WiringCount + " time(s)";           
        }

        private void unwire2Button_Click(object sender, EventArgs e)
        {
            _consumer2.UnWireDelegate();
            consumer2TextBox.Text = "Consumer2 is wired for " + _consumer2.WiringCount + " time(s)";            
        }


        private void reset1Button_Click(object sender, EventArgs e)
        {
            _consumer1.ResetDelegateToNull();
            issueTextBox.Text = "Resetting Consumer1 wiring will reset the wiring of Consumer2 having wiring" + _consumer2.WiringCount + " time(s)";
            //_consumer2.ResetWiringCount(); reset the wiring count to zero. As consumer1 has set the Opearate delegate to null, that will indirectly set consumer2 Operate to null
        }
        private void reset2Button_Click(object sender, EventArgs e)
        {
            _consumer2.ResetDelegateToNull();
            issueTextBox.Text = "Resetting Consumer2 wiring will reset the wiring of Consumer1 having wiring" + _consumer1.WiringCount + " time(s)";
            //_consumer2.ResetWiringCount();
        }
      
    }

    //Delegate defination
    //It is exactly similar to declaring a signature of method.
    //You need to use delegate as suffix to signature
    //OperationDelegate is the Name of delegate
    public delegate void OperationDelegate();

    public class MyData
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public OperationDelegate Operate { get; set; }
    }


    public class Consumer
    {
        private MyData _data;
        private string _name;
        private int _wiringCount;
        public int WiringCount
        {
            get
            {
                return _wiringCount;
            }
        }
        public Consumer(MyData data,string name)
        {
            _data = data;
            _name = name;
        }
        private void Operation()
        {
            MessageBox.Show("Operation method of instance "+_name+" is excuted.");
        }
        public void ResetDelegateToNull()
        {
            _wiringCount = 0;
            _data.Operate = null;
        }
        public void WireDelegate()
        {
            _wiringCount++;
            _data.Operate += this.Operation;
        }
        public void UnWireDelegate()
        {
            if (_wiringCount > 0)
            _wiringCount--;
            _data.Operate -= this.Operation;
        }

        public void ResetWiringCount()
        {
            _wiringCount = 0;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions