I have a list like this:
List list = new List
How to handle adding new position to this list?
When
You could inherit from List and add your own handler, something like
using System;
using System.Collections.Generic;
namespace test
{
class Program
{
class MyList<T> : List<T>
{
public event EventHandler OnAdd;
public new void Add(T item) // "new" to avoid compiler-warnings, because we're hiding a method from base-class
{
if (null != OnAdd)
{
OnAdd(this, null);
}
base.Add(item);
}
}
static void Main(string[] args)
{
MyList<int> l = new MyList<int>();
l.OnAdd += new EventHandler(l_OnAdd);
l.Add(1);
}
static void l_OnAdd(object sender, EventArgs e)
{
Console.WriteLine("Element added...");
}
}
}
Be aware that you have to re-implement all methods which add objects to your list. AddRange()
will not fire this event, in this implementation.
We did not overload the method. We hid the original one. If you Add()
an object while this class is boxed in List<T>
, the event will not be fired!
MyList<int> l = new MyList<int>();
l.OnAdd += new EventHandler(l_OnAdd);
l.Add(1); // Will work
List<int> baseList = l;
baseList.Add(2); // Will NOT work!!!
To piggy-back off Ahmad's use of Extension Methods, you can create your own class where the list is private with a public get
method and a public add
method.
public class MyList
{
private List<SomeClass> PrivateSomeClassList;
public List<SomeClass> SomeClassList
{
get
{
return PrivateSomeClassList;
}
}
public void Add(SomeClass obj)
{
// do whatever you want
PrivateSomeClassList.Add(obj);
}
}
However, this class only provides access to List<>
methods that you manually expose...hence may not be useful in cases where you need a lot of that functionality.
One simple solution is to introduce an Add method for the list in your project and handle the event there. It doesn't answer the need for an event handler but can be useful for some small projects.
AddToList(item) // or
AddTo(list,item)
////////////////////////
void AddTo(list,item)
{
list.Add(item);
// event handling
}
instead of
list.Add(item);
No need for adding an event just add the method.
public class mylist:List<string>
{
public void Add(string p)
{
// Do cool stuff here
base.Add(p);
}
}
//List overrider class
public class ListWithEvents<T> : List<T>
{
public delegate void AfterChangeHandler();
public AfterChangeHandler OnChangeEvent;
public Boolean HasAddedItems = false;
public Boolean HasRemovedItems = false;
public bool HasChanges
{
get => HasAddedItems || HasRemovedItems;
set
{
HasAddedItems = value;
HasRemovedItems = value;
}
}
public new void Add(T item)
{
base.Add(item);
HasAddedItems = true;
if(OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void AddRange(IEnumerable<T> collection)
{
base.AddRange(collection);
HasAddedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void Insert(int index,T item)
{
base.Insert(index, item);
HasAddedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void InsertRange(int index, IEnumerable<T> collection)
{
base.InsertRange(index, collection);
HasAddedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void Remove(T item)
{
base.Remove(item);
HasRemovedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void RemoveAt(int index)
{
HasRemovedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void RemoveRange(int index,int count)
{
HasRemovedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public new void Clear()
{
base.Clear();
HasRemovedItems = true;
if (OnChangeEvent != null) OnChangeEvent();
OnChange();
}
public virtual void OnChange()
{
}
}
You can't do this with List<T>
. However, you can do it with ObservableCollection<T>
. See ObservableCollection<T> Class.