Wednesday, July 22, 2009

Creating Delegates Using C#.net

Creating Of Deletecates Events

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace delegates
{
  ///


  /// Summary description for Delegates
  ///

  public class Delegates : System.Collections.ArrayList
  {
  public event System.EventHandler changed;

  protected virtual void Onchanged(System.EventArgs e)
  {
  if (changed != null)
  {
  changed(this, e);
  }
  }
  public override int Add(object value)
  {
  int i = base.Add(value);
  Onchanged(System.EventArgs.Empty);
  return i;

  }
  public Delegates()
  {

  //
  // TODO: Add constructor logic here
  //
  }
  }

}

Creation Of EventHander and Fucntions

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///


/// Summary description for delegates_list
///

/// 
using delegates;
namespace delegates_list_pro
{
public class delegates_list
{
  private Delegates list1;
  public delegates_list(Delegates alist)
  {
  list1=alist;
  list1.changed+=new System.EventHandler(listadded);
   
  }
  private void listadded(object sender,EventArgs e)
  {
  System.Windows.Forms.MessageBox.Show("WELCOME");
  }
  public void Detach()
  {
  list1.changed-=new System.EventHandler(listadded);
  list1=null;
  }



 public delegates_list()
 {
  //
  // TODO: Add constructor logic here
  //
 }
}
}

Calling Detlegates and its event handlers from the main program

using delegates;
using delegates_list_pro;

Delegates dele = new Delegates();
delegates_list listener = new delegates_list(dele);
dele.Add("Welcome");
dele.Clear();
listener.Detach();

No comments: