Popular Posts

Sunday, February 13, 2011

Delegates Made Easy


using ....   //include all the libraries you want to include

namespace trialprograms
{
    public partial class Form1 : Form
    { 
        //declare a delegate
        public delegate void Del(string message); 
        //Del is the name of the delegate
        //x,y are the arguments to the delegate
        //int is the return type of the delegate

        private void DelegateMethod(string message)
        {

            MessageBox.Show(message);
        }

        public void func_checkdelegate(int x,int y,Del callback)
        {
            //invoke the delegate and send the parameters
            callback("The sum of the two numbers is "+ (x + y).ToString());
        }

public Form1()
        {
            InitializeComponent();
            //instantiate the delegate
            //Here it points to the function Delegate Method
            Del handler = DelegateMethod;  //the return type the signature of the delegate and the function it       points to must match
            //sending the  object of the delegate with the function
            func_checkdelegate(2,3,handler);
            
           }
 }

1 comment: