Changes to the listener list (arraylist)

Source: Internet
Author: User
Using System;
Namespace Mycollections
{
Using System. collections;

// The delegate type used to hook the change notification.
Public   Delegate   Void Changedeventhandler ( Object Sender, eventargs E ); // Changedeventhandler can be used here, instead of the system-defined delegate eventhandler.

// A class, similar to arraylist,
// However, a notification is sent each time the list is changed.
Public   Class Listwithchangedevent: arraylist
{
// An event that the client can use whenever the list element changes
// Get notification.
Public   Event Changedeventhandler changed;

// Call the changed event. It is called whenever the list is changed.
Protected   Virtual   Void Onchanged (eventargs E)
{
If(Changed! = Null)
Changed (
This, E );
}


// Override some methods for changing the list;
// Call the event after each rewrite
Public   Override   Int Add ( Object Value)
{
IntI= Base. Add (value );
Onchanged (eventargs. Empty );
ReturnI;
}


Public   Override   Void Clear ()
{
Base. Clear ();
Onchanged (eventargs. Empty );
}


Public   Override   Object   This [ Int Index]
{
Set  
{
Base[Index]=Value;
Onchanged (eventargs. Empty );
}

}

}

}


Namespace Testeven TS
{
Using Mycollections;

Class Eventlistener
{
Private Listwithchangedevent list;

Public Eventlistener (listwithchangedevent List)
{
List
=List;
//Add "listchanged" to the changed event in "list.
List. Changed+ = NewChangedeventhandler (listchanged );
}


// The following call is performed whenever the list is changed.
Private   Void Listchanged ( Object Sender, eventargs E)
{
Console. writeline (
"This is called when the event fires.");
}


Public   Void Detach ()
{
//Detach events and delete the list
List. Changed-= NewChangedeventhandler (listchanged );
List
= Null;
}

}


Class Test
{
// Test the listwithchangedevent class.
Public   Static   Void Main ()
{
// Create a new list.
Listwithchangedevent list =   New Listwithchangedevent ();

// Create a class to listen for the change event of the list.
Eventlistener listener =   New Eventlistener (list );

// Add and remove items in the list.
List. Add ( " Item 1 " );
List. Clear ();
Listener. Detach ();
}

}

}

Mao's nest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.