• 0

[C#] Need to raise event on Add() method for List<T>


Question

I have a variable declared as List<Panel> and I need to raise an event every time I add a panel to the list and I need to do this in the same class has the variable is declared (just beucase I tried to subclass Collection<T> and overriding InsertItem(), but wasn't able to access variables/methods on the same class ehere the Collection<Panel> would be declared.

Can anyone help me out? How can I raise an event on the Add() method of List<T>?

8 answers to this question

Recommended Posts

  • 0

Because you can't override the Add() method of the Generic List<T> class, trying to add events to it will be useless. What you can do, is create a class which extends the System.Collections.CollectionBase class. The CollectionBase class providers an internal collection, to which you add your own Add(), Remove(), Insert() etc. methods to create a externally-strong collection. With the code below, I am defining my custom collection, with an event which can be fired when a string is added to the collection:

public class StringCollection : CollectionBase
	{
		public delegate void StringCollectionEventHandler(object sender, StringEventArgs e);
		public event StringCollectionEventHandler StringAdded;

		public int Add(string item)
		{
			int index = this.InnerList.Add(item);
			if (StringAdded != null)
			{
				StringAdded(this, new StringEventArgs(item));
			}
			return index;
		}
	}

	public class StringEventArgs {
		private string value = string.Empty;

		internal StringEventArgs(string value)
		{
			this.value = value;
		}

		public string Value { get { return value; } }
	}

I make sure I declare both the collection, and my event arguments that I want the event to carry. What happens here is that when the user attaches an event to my collection, my collection will fire that event, passing through the item that was added:

class Program
	{
		static void Main(string[] args)
		{
			StringCollection collection = new StringCollection();
			collection.StringAdded += new StringCollection.StringCollectionEventHandler(collection_StringAdded);
			collection.Add("This		collection.Add("Is		collection.Add("A		collection.Add("Test		collection.Add("String		Console.ReadKey();
		}

		static void collection_StringAdded(object sender, StringEventArgs e)
		{
			Console.WriteLine(string.Format("'{0}' was added to the collection", e.Value));
		}
	}

  • 0

While searching on how to override the Add() method of List<T>, I found someone suggestion overriding the InsertItem() method of Collection<T>. And it works as I wanted. The only problem I was having, was with delegates and events and how would I call them from a different class. But I managed to do it...

Thanks anyway for your help Antaris! :)

  • 0

I see you've already found a solution, but had you considered using ObservableCollection<T>?

It has a built in CollectionChanged event that might be useful for you. This appears to only exist in .NET 3.0, so if you are still only using 2.0, then it really won't help you. If your use of delegates is sufficient, then ignore this, but I thought it might come in handy.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.