Click here to Skip to main content
12,446,296 members (53,897 online)
Click here to Skip to main content
Add your own
alternative version

Stats

24.6K views
167 downloads
22 bookmarked
Posted

List With Events

, 6 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An extended List that fires events when the count of list-items changes

Introduction

List<T> provides no events when the count of the list-items is changed. This article provides a class that notifies with a event when the number of the elements is changed.

The class provides a basic approach to this. Further members can be added if needed.

Using the Code

An example will show the usage of this class:

using System;
using gfoidl.Tools;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main()
		{
			gfList<int> list = new gfList<int>();
			list.CountChanged += 
				new EventHandler<gfList<int>.ListEventArgs>
					(list_CountChanged);

			list.Add(1);
			list.AddRange(new int[] { 2, 3, 4, 5 });
			list.Remove(3);
			list.Clear();

			Console.ReadKey();
		}

		private static void list_CountChanged(
			object sender, 
			gfList<int>.ListEventArgs e)
		{
			Console.WriteLine(e.Count);
		}
	}
}

Implementation

using System;
using System.Collections.Generic;

namespace gfoidl.Tools
{
	/// <summary>
	/// Extended list with events
	/// </summary>
	/// <typeparam name="T">
	/// The type of the elements
	/// </typeparam>
	public class gfList<T> : List<T>
	{
		#region Event(s)
		public event EventHandler<ListEventArgs> CountChanged;
		#endregion
		//---------------------------------------------------------------
		#region Methods
		/// <summary>
		/// Adds an item
		/// </summary>
		public new void Add(T item)
		{
			base.Add(item);

			// Copy to a temporary variable to be thread-safe (MSDN).
			EventHandler<ListEventArgs> tmp = CountChanged;
			if (tmp != null)
			    tmp(this, new gfList<T>.ListEventArgs(this.Count));
		}
		//------------------------------------------------------------------
		/// <summary>
		/// Adds a range
		/// </summary>
		public new void AddRange(IEnumerable<T> collection)
		{
			base.AddRange(collection);

			// Copy to a temporary variable to be thread-safe (MSDN).
			EventHandler<ListEventArgs> tmp = CountChanged;
			if (tmp != null)
			    tmp(this, new gfList<T>.ListEventArgs(this.Count));
		}
		//------------------------------------------------------------------
		/// <summary>
		/// Clears the list.
		/// </summary>
		public new void Clear()
		{
			base.Clear();

			// Copy to a temporary variable to be thread-safe (MSDN).
			EventHandler<ListEventArgs> tmp = CountChanged;
			if (tmp != null)
			    tmp(this, new gfList<T>.ListEventArgs(this.Count));
		}
		//------------------------------------------------------------------
		/// <summary>
		/// Removes the first matched item.
		/// </summary>
		public new void Remove(T item)
		{
			base.Remove(item);

			// Copy to a temporary variable to be thread-safe (MSDN).
			EventHandler<ListEventArgs> tmp = CountChanged;
			if (tmp != null)
			    tmp(this, new gfList<T>.ListEventArgs(this.Count));
		}
		#endregion
		//------------------------------------------------------------------
		#region Subclasses
		/// <summary>
		/// EventArgs
		/// </summary>
		public class ListEventArgs : EventArgs
		{
			/// <summary>
			/// Number of elements in the list
			/// </summary>
			public int Count { get; set; }
			//----------------------------------------------------------
			public ListEventArgs(int anzahl)
			{
				this.Count = anzahl;
			}
		}
		#endregion
	}
}

History

  • 6th December, 2008: Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Günther M. FOIDL
Software Developer (Senior) Foidl Günther
Austria Austria
Engineer in combustion engine development.
Programming languages: C#, FORTRAN 95, Matlab

FIS-overall worldcup winner in Speedski (Downhill) 2008/09 and 2009/10.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 21:31
membermanoj kumar choubey26-Feb-12 21:31 
GeneralFor when you only want to know if a list is modified: Pin
tonyt23-Dec-08 5:48
membertonyt23-Dec-08 5:48 
GeneralRe: For when you only want to know if a list is modified: Pin
Günther M. FOIDL19-Jun-09 3:12
memberGünther M. FOIDL19-Jun-09 3:12 
GeneralSimple Solution, but there are issues with using Hiding as the Implementation Pin
Jason Smith - FMR10-Dec-08 3:42
memberJason Smith - FMR10-Dec-08 3:42 
GeneralGood Work Pin
tonyt8-Dec-08 21:13
membertonyt8-Dec-08 21:13 
GeneralINotifyPropertyChanging / INotifyPropertyChanged Pin
Marc Sommer7-Dec-08 8:33
memberMarc Sommer7-Dec-08 8:33 
GeneralRe: INotifyPropertyChanging / INotifyPropertyChanged Pin
Günther M. FOIDL7-Dec-08 9:48
memberGünther M. FOIDL7-Dec-08 9:48 
GeneralNice Pin
Maximilian Korporal6-Dec-08 5:14
memberMaximilian Korporal6-Dec-08 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160811.3 | Last Updated 6 Dec 2008
Article Copyright 2008 by Günther M. FOIDL
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid