15.3. A full example

After examining the various pieces of the whole picture, it is time to put them all together. Here is a fully coded example consisting of three classes – the source, the consumer, and an execution class containing the Main()method. To keep things simple, I am not going to write a custom-made subclass of EventArgs for the event object, and I have purposely placed the Main() method in a separate class so that the role of each class can be clearly seen.

Lines 1 – 7 code the delegate declaration:

1:  using System;
2:
3:  /* -----------------------------------------------
4:   * Delegate declaration
5:   *--------------------------------------------- */
6:  delegate void MyEventHandler (object sender, EventArgs e);
7:

Lines 8 – 29 code the event source class. Line 12 declares the event instance called SomethingHappened. This is of delegate type MyEventHandler. In the example here, when the Run() method is called, it produces and prints out 200 random ints between 0 and 99 (lines 20 – 21) in a for loop. Take note of lines 24 – 26 – in this trivial example, we are interested in the event whereby a 9 appears as one of the random ints. When such an event occurs, the SomethingHappened event is fired.

 8:  /* ----------------------------------------------
 9:   * Event Source
10:   * --------------------------------------------*/
11: class MySource{
12:    public event MyEventHandler SomethingHappened;
13:
14:    public void Run(){
15:      // produce random numbers
16:      Random r = new Random();
17:      int rnd;
18:
19:      for (int i=0; i<200; i++){
20:        rnd = r.Next() % 100; // between 0 and 99
21:        Console.Write(rnd + ", ");
22:
23:        // event happened! Fire event
24:        if (rnd= =9){
25:          SomethingHappened(this, new EventArgs());
26:        }
27:      }
28:    } // end Run
29:  } // end class
30:

Lines 31 – 39 code the event consumer. It contains only one event handler method called TellMe. TellMe's return type and parameters must match that of delegate type MyEventHandler in order for this event handler to be registered with the source's SomethingHappened event. This trivial event handler simply prints out a message.

31:  /* ----------------------------------------------
32:   * Event consumer
33:   * ---------------------------------------------*/
34:  class MyConsumer{
35:    // event handler
36:    public void TellMe (object sender, EventArgs e){
37:      Console.Write("***** i am being notified *****");
38:    }
39:  }
40:

Lines 41 – 53 code the main class where the action happens. On lines 47 – 48, new consumer and source objects are created. Event registration is performed on lines 49 – 50 – here the event handler of consumer is 'hooked to' the SomethingHappened event, so that when SomethingHappened fires consumer.TellMe runs. Finally, on line 51, Run() of the source object is invoked, so that random number generation starts.

41:  /* ---------------------------------------
42:   * Main class
43:   * --------------------------------------*/
44:  class TestClass{
45:
46:    public static void Main(){
47:      MyConsumer consumer = new MyConsumer();
48:      MySource source = new MySource();
49:      source.SomethingHappened +=
50:        new MyEventHandler(consumer.TellMe);
51:      source.Run();
52:    }
53:  }

Output: [2]

[2] Of course your output will vary because the numbers generated are random.

c:expt>test
4, 78, 65, 66, 64, 20, 59, 27, 28, 29, 34, 68, 21, 12, 98, 56, 64, 52, 42, 93, 44, 40, 6,
 14, 31, 96, 83, 48, 96, 25, 59, 86, 17, 58, 68, 70, 78, 21, 47, 88, 24, 86, 79, 69, 95, 35
, 68, 9, ***** i am being notified ***** 63, 40, 48, 32, 1, 66, 74, 64, 19, 51, 82, 68, 37,
 58, 31, 50, 70, 48, 51, 10, 91, 28, 25, 90, 5, 1, 69, 5, 8, 37, 66, 96, 28, 74, 32, 56, 24
, 74, 85, 98, 31, 4, 98, 27, 86, 26, 51, 13, 55, 76, 99, 47, 31, 5, 65, 35, 62, 5, 27, 47,
 97, 69, 3, 82, 32, 33, 40, 10, 73, 22, 73, 43, 10, 53, 79, 34, 77, 98, 4, 26, 50, 3, 97,
 79, 38, 19, 65, 70, 56, 97, 94, 19, 47, 85, 1, 62, 1, 16, 95, 0, 86, 88, 87, 33, 3, 3, 84,
 78, 73, 31, 58, 11, 1, 48, 97, 41, 19, 71, 91, 13, 68, 17, 54, 76, 28, 54, 43, 25, 99, 64,
 33, 8, 3, 51, 87, 9, ***** i am being notified ***** 63, 64, 76, 82, 82, 34, 44, 72, 86,
 30, 65, 46, 88, 60, 43, 77,

Observe the output. When Run() executes and produces random ints, a SomethingHappened event gets fired when a 9 appears. The event handler TellMe() runs and prints out a message.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.181