C#
  Home arrow C# arrow Page 2 - Event Handling With C#
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Event Handling With C#
By: Deepak Dutta
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 23
    2002-07-23

    Table of Contents:
  • Event Handling With C#
  • Delegates in C#
  • Event Handlers in C#
  • GUI Event Handling
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Event Handling With C# - Delegates in C#


    (Page 2 of 5 )

    A delegate in C# allows you to pass methods of one class to objects of other classes that can call those methods. You can pass method m in Class A, wrapped in a delegate, to class B and Class B will be able to call method m in class A. You can pass both static and instance methods.

    This concept is familiar to C++ developers who have used function pointers to pass functions as parameters to other methods in the same class or in another class. The concept of a delegate was introduced in Visual J++, and then carried over to C#. C# delegates are implemented in the .Net framework as a class derived from System.Delegate. The use of a delegate involves four steps:
    1. Declare a delegate object with a signature that exactly matches the method signature that you are trying to encapsulate.
    2. Define all the methods whose signatures match the signature of the delegate object that you have defined in step 1.
    3. Create a delegate object and plug-in the methods that you want to encapsulate.
    4. Call the encapsulated methods through the delegate object.
    The following C# code shows the above four steps implemented using one delegate and four classes. Your implementation will vary depending on the design of your classes:

    using System;

    //Step 1. Declare a delegate with the signature of the encapsulated method
    public delegate void MyDelegate(string input);

    //Step 2. Define methods that match with the signature of delegate declaration
    class MyClass1{
    public void delegateMethod1(string input){
    Console.WriteLine("This is delegateMethod1 and the input to the method is {0}",input);
    }
    public void delegateMethod2(string input){
    Console.WriteLine("This is delegateMethod2 and the input to the method is {0}",input);
    }
    }

    //Step 3. Create delegate object and plug in the methods
    class MyClass2{
    public MyDelegate createDelegate(){
    MyClass1 c2=new MyClass1();
    MyDelegate d1 = new MyDelegate(c2.delegateMethod1);
    MyDelegate d2 = new MyDelegate(c2.delegateMethod2);
    MyDelegate d3 = d1 + d2;
    return d3;
    }
    }

    //Step 4. Call the encapsulated methods through the delegate
    class MyClass3{
    public void callDelegate(MyDelegate d,string input){
    d(input);
    }
    }

    class Driver{
    static void Main(string[] args){
    MyClass2 c2 = new MyClass2();
    MyDelegate d = c2.createDelegate();
    MyClass3 c3 = new MyClass3();
    c3.callDelegate(d,"Calling the delegate");
    }
    }

    More C# Articles
    More By Deepak Dutta


       · Author says "Declare a delegate object with a signature that exactly matches the...
     

    C# ARTICLES

    - Introduction to Objects and Classes in C#, P...
    - Visual C#.NET, Part 1: Introduction to Progr...
    - C# - An Introduction
    - Hotmail Exposed: Access Hotmail using C#
    - Razor Sharp C#
    - Introduction to Objects and Classes in C#
    - Making Your Code CLS Compliant
    - Programming with MySQL and .NET Technologies
    - Socket Programming in C# - Part II
    - Socket Programming in C# - Part I
    - Creational Patterns in C#
    - Type Conversions
    - Creating Custom Delegates and Events in C#
    - Inheritance and Polymorphism
    - Understanding Properties in C#







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek