xref: /Universal-ctags/Units/csharp-review-needed.r/events.cs.t/input.cs (revision 82d25c4a1fcdbb412df0a6cd190e9736b6e4c049)
1 // events1.cs
2 using System;
3 namespace MyCollections
4 {
5    using System.Collections;
6 
7    // A delegate type for hooking up change notifications.
ChangedEventHandler(object sender, EventArgs e)8    public delegate void ChangedEventHandler(object sender, EventArgs e);
9 
10    // A class that works just like ArrayList, but sends event
11    // notifications whenever the list changes.
12    public class ListWithChangedEvent: ArrayList
13    {
14       // An event that clients can use to be notified whenever the
15       // elements of the list change.
16       public event ChangedEventHandler Changed;
17 
18       // Invoke the Changed event; called whenever list changes
OnChanged(EventArgs e)19       protected virtual void OnChanged(EventArgs e)
20       {
21          if (Changed != null)
22             Changed(this, e);
23       }
24 
25       // Override some of the methods that can change the list;
26       // invoke event after each
Add(object value)27       public override int Add(object value)
28       {
29          int i = base.Add(value);
30          OnChanged(EventArgs.Empty);
31          return i;
32       }
33 
Clear()34       public override void Clear()
35       {
36          base.Clear();
37          OnChanged(EventArgs.Empty);
38       }
39 
40       public override object this[int index]
41       {
42          set
43          {
44             base[index] = value;
45             OnChanged(EventArgs.Empty);
46          }
47       }
48    }
49 }
50 
51 namespace TestEvents
52 {
53    using MyCollections;
54 
55    class EventListener
56    {
57       private ListWithChangedEvent List;
58 
EventListener(ListWithChangedEvent list)59       public EventListener(ListWithChangedEvent list)
60       {
61          List = list;
62          // Add "ListChanged" to the Changed event on "List".
63          List.Changed += new ChangedEventHandler(ListChanged);
64       }
65 
66       // This will be called whenever the list changes.
ListChanged(object sender, EventArgs e)67       private void ListChanged(object sender, EventArgs e)
68       {
69          Console.WriteLine("This is called when the event fires.");
70       }
71 
Detach()72       public void Detach()
73       {
74          // Detach the event and delete the list
75          List.Changed -= new ChangedEventHandler(ListChanged);
76          List = null;
77       }
78    }
79 
80    class Test
81    {
82       // Test the ListWithChangedEvent class.
Main()83       public static void Main()
84       {
85       // Create a new list.
86       ListWithChangedEvent list = new ListWithChangedEvent();
87 
88       // Create a class that listens to the list's change event.
89       EventListener listener = new EventListener(list);
90 
91       // Add and remove items from the list.
92       list.Add("item 1");
93       list.Clear();
94       listener.Detach();
95       }
96    }
97 }
98 
99 // events2.cs
100 using System;
101 namespace MyCollections
102 {
103    using System.Collections;
104 
105    // A class that works just like ArrayList, but sends event
106    // notifications whenever the list changes:
107    public class ListWithChangedEvent: ArrayList
108    {
109       // An event that clients can use to be notified whenever the
110       // elements of the list change:
111       public event EventHandler Changed;
112 
113       // Invoke the Changed event; called whenever list changes:
OnChanged(EventArgs e)114       protected virtual void OnChanged(EventArgs e)
115       {
116          if (Changed != null)
117             Changed(this,e);
118       }
119 
120       // Override some of the methods that can change the list;
121       // invoke event after each:
Add(object value)122       public override int Add(object value)
123       {
124          int i = base.Add(value);
125          OnChanged(EventArgs.Empty);
126          return i;
127       }
128 
Clear()129       public override void Clear()
130       {
131          base.Clear();
132          OnChanged(EventArgs.Empty);
133       }
134 
135       public override object this[int index]
136       {
137          set
138          {
139             base[index] = value;
140             OnChanged(EventArgs.Empty);
141          }
142       }
143    }
144 }
145 
146 namespace TestEvents
147 {
148    using MyCollections;
149 
150    class EventListener
151    {
152       private ListWithChangedEvent List;
153 
EventListener(ListWithChangedEvent list)154       public EventListener(ListWithChangedEvent list)
155       {
156          List = list;
157          // Add "ListChanged" to the Changed event on "List":
158          List.Changed += new EventHandler(ListChanged);
159       }
160 
161       // This will be called whenever the list changes:
ListChanged(object sender, EventArgs e)162       private void ListChanged(object sender, EventArgs e)
163       {
164          Console.WriteLine("This is called when the event fires.");
165       }
166 
Detach()167       public void Detach()
168       {
169          // Detach the event and delete the list:
170          List.Changed -= new EventHandler(ListChanged);
171          List = null;
172       }
173    }
174 
175    class Test
176    {
177       // Test the ListWithChangedEvent class:
Main()178       public static void Main()
179       {
180       // Create a new list:
181       ListWithChangedEvent list = new ListWithChangedEvent();
182 
183       // Create a class that listens to the list's change event:
184       EventListener listener = new EventListener(list);
185 
186       // Add and remove items from the list:
187       list.Add("item 1");
188       list.Clear();
189       listener.Detach();
190       }
191    }
192 }
193