delegate & event

This is a discussion on delegate & event within the C# Programming forums, part of the General Programming Boards category; Hi, I'm beginner in C#. If someone can explain me step by step what this code do? Code: public delegate ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    delegate & event

    Hi,
    I'm beginner in C#.
    If someone can explain me step by step what this code do?

    Code:
    public delegate void pqr(int p);
    class z {	static void Main(){
    x a; a = new x();
    a.c += new pqr(a.pqr1); a.abc();
    a.c += new pqr(a.pqr2); a.abc();}}
    
    class x {	
    public void pqr1(int j){ System.Console.WriteLine("pqr1 " + j); }
    public void pqr2(int j){ System.Console.WriteLine("pqr2 " + j); }
    public event pqr c;
    public void abc() { c(10); }}
    I'm pretty uncomfortable with event, how it works?
    Thanks!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The event model means you have a class that has events ( for example a button that can be clicked. The Click is an event ). Now you can register so-called Event-Handlers with these events ( in this example a function that gets called when the button gets clicked ). A single event my have more than one event handler attached. Each attached event handler will be called. However, there is no fixed sequence, the order of the event handlers can be random ( example: if you attach two functions to the click of the button, you can be sure both will be called when the button is clicked. However, you can never be sure which one will be called first ).

    Code:
    public delegate void pqr(int p);
    This is a declaration of a delegate. In C++ this would be a function pointer. It's a type which can encapsulate functions so that these functions can later be called.

    some class to have a main method:

    Code:
    class z
    {
    static void Main()
    {
    Create a new instance of class x, as described later

    Code:
    x a;
    a = new x();
    This class has an event c. To attach an event handler to this event, we create a new delegate ( see type above ) with the instance and function to call.

    Code:
    a.c += new pqr(a.pqr1);
    Now there is one event handler attached to the event. We call a method on the class that will trigger the event. The following line will result in a.pqr1 getting called because this was attached as an eventhandler for the event that will occur

    Code:
    a.abc();
    We will now add a second event handler. As I said earlier, BOTH will get called, but not neccessarily in any specific order

    Code:
    a.c += new pqr(a.pqr2);
    We raise the event again, the result should be that BOTH, a.pqr1 AND a.pqr2 are called, maybe 1 first, maybe the other first, thats not defined.

    Code:
    a.abc();
    }
    }
    This class contains the event

    Code:
    class x
    {	
    public void pqr1(int j)
    { System.Console.WriteLine("pqr1 " + j); }
    
    public void pqr2(int j)
    { System.Console.WriteLine("pqr2 " + j); }
    The latter where just two normal functions printing the int that was given as a parameter. The following is the declaration of an event. It's type is declared above. This means: This class has an event, which can be handled by functions that match the type of pqr ( earlier defined as delegate to functions that return void and take an int ).

    Code:
    public event pqr c;
    The following function will raise the event with the number 10, which will result in all attached event handler functions getting called with 10 as parameter.

    Code:
    public void abc()
    {
     c(10); 
    }
    
    }

    I have some personal comments for this file: If you did it yourself for testing, it's ok, you're still learning and I don't mean to criticise you. If this was a tutorial or help of some kind, it's badly written. For one, Eventhandlers always have two parameters, and object named sender and an EventArgs parameter named e ( Microsoft Class library design guidelines ). However, this is just a definition and not everyone might want to take that serious. But the event is called without any checks by the abc function. While in this example it works, it will crash if you don't have any event handlers attached. As the nature of events is that the sender will not know if or how they will be handled, checking this is a basic neccessity.

    replace c(10); by if( c != null ) c(10);

    This sample is a bit too complex. You only need one class to demonstrate event handlers and having the event and the handler in the same class is perfectly valid, but maybe a bit confusing as it's against the event/handler concept.

    If this was a tutorial, get a way better one. If it was you testing stuff out, just disregard my ramblings
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    This example is from tutorial and as you can see it is bad!
    Thank you very much!

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    If you or nyone else is looking for a better tutorial, I found www.csharp-station.com to be quite useful. Their tutorial on events and delegates was a bit tough too, as it's a tough subject to cover, but this helped a lot. Everything else on that site is quite good, though.

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    while we are on the subject of delegates...

    it has already been stated very many times that they are like function pointers in essence.

    i have been attempting to use delegates to create arrays of functions, just like you can use function pointers to create arrays of functions in C++, but so far i have not been successful.

    anyone know of how to do that with delegates?

    it isn't as simple as it seems...either that or i am just stupid...
    My Website

    "Circular logic is good because it is."

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I have never tried this, so if something unexpected happens, let me know


    public delegate void FunctionPointerEquivalent( int i );

    public FunctionPointerEquivalent[] array = new FunctionPointerEquivalent[50];

    now you should have 50 nulls. Create each one with

    array[x] = new FunctionPointerEquivalent( yourfunctionname );

    The only reason for it to fail might be that a delegate has no empty constructor for the array... but just try it, I don't have a compiler at hand on this system
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Events
    By siavoshkc in forum C# Programming
    Replies: 1
    Last Post: 10-02-2006, 03:43 AM
  3. event vs delegate
    By antex in forum C# Programming
    Replies: 0
    Last Post: 05-31-2006, 03:27 PM
  4. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21