/*C# Events
______________
Events notify the program when the state of the control changes.*/
/*Declaring an Event:
Events are decalared by uding a delegate object. A delegate object encapsulates
a method so that it can be called anonymously from the application. When an event
occurs, the client application invokes the corresponding delegate for the event
to which the delegate is attached.
Before you declare an event inside a class you need to declare a delegate by
using the delegate keyword. For every event in C#, there is a corresponding
method declaration that handles it.*/
public delegate void ModifiedEventhandler();
/*The delegate type consists of the set of arguments that it passes to the method
that handles the event. Multiple events can share the same delegate. In other
words by using a single delegate, you can invoke different methods corresponding
to different events.*/
public event ModidifedEventhandler Changed;
/*The event is declared using the event keyword. The event keyword follows the
access modifiers. Events are usually public, but you can specify any access modifier.
The event is declared as a field of the delegate type. The above example displays
the declaration of the event named changed as a field of the ModifiedEventHandler
delegate type.*/
if(Changed != null)
Changed();
/*After you declare an event in a class, the class treats the event as a field of
the declared delegate. Declaring events as a field of the delegate type allows
you to call the event directly like a method. The field Changed refers to a
delegate that is called when the event occurs. The above example shows that an
event is involved after checking whether or not the event is null. You invoke
an event only when it's not null.*/
You can perform the following operations on an event that is involved
1. Compose a new delegate onto that field
2. Remove a delegate from a composed field
List.Changed += new ChangedEventHandler(ListChanged);
List.Changed -= new ChangedEventHandler(ListChanged);
/*You composed a delegate by using the + operator.You remove a delegate by using
the - operator. To receive the event invocation, the client application, the
application that raises the event, creates a delegate of the event type. This
event refers to the method that is called when the event in invoked. Then, the
client application composes the new delegate with the existing delegate in the
event. When the client application no longer receives event invocations, it
removes the delegates from the event.*/
protected virtual void onChanged()
{
if(Changed != null)
Changed();
}
public override int Add(object value)
{
int i = base.Add(value);
onChanged();
return i;
}
/*You can invoke events only within the class in which the event is decalred.
You cannot directly call the events declared in the base class from the
derived class. To call a base class event from derived class, you create a
protected method in the application that invokes the event. For Example: the
state of a list box changes whenever you add an item to the list or delete an
item from the list.*/