Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an existing project that is using data sets as a storing mechanism.
I want to migrate to Entity Framework 4.1 Code First.
My classes contain strongly typed collection classes, like this:

VB
Public Class Calculations

  Inherits CollectionBase
  Public Event CalculationAdded(ByVal Calculation As Calculation)

#Region "Strongly Typed Collection Methods"
  Default Public Overloads Property Item(ByVal index As Integer) As Calculation
    Get
      Return CType(Me.List.Item(index), Calculation)
    End Get
    Set(ByVal value As Calculation)
      Me.List.Item(index) = value
    End Set
  End Property

  Public Function Add(ByVal value As Calculation) As Integer
    RaiseEvent CalculationAdded(value)
    Return Me.List.Add(value)
  End Function
  Public Overloads Function Contains(ByVal obj As Calculation) As Boolean
    Return Me.List.Contains(obj)
  End Function
  Public Overloads Function IndexOf(ByVal obj As Calculation) As Integer
    Return Me.List.IndexOf(obj)
  End Function
  Public Overloads Sub Remove(ByVal obj As Calculation)
    Me.RemoveAt(IndexOf(obj))
  End Sub
  Public Overloads Sub Sort()
    Me.InnerList.Sort()
  End Sub
#End Region

End Class


In the code examples of code first, everyone uses List(Of T) to store a collection of some type. But that way, I can't implement events, I think.

I Googled a lot, but it doesn't get me anywhere. Any advise would be helpful.

Can I use List(Of T) and still be able to fire events when an item is added to the collection?
Posted
Updated 19-Jan-12 1:42am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 21:22pm    
Not a question. You cannot implement something, so what? Learn events and other stuff and try again. Our possibilities to help where education is needed are limited. When you face with particular problems, we will gladly help you.
--SA
Arend Jan 20-Jan-12 2:08am    
How is:
"Can I use List(Of T) and still be able to fire events when an item is added to the collection?"
not a question?
If someone knows less than you do, you could point him in the right direction. I don't expect you to do my coding.

1 solution

Hi!
So you want to make your collections "observable". You can use some of them provided (available) in .NET 4.0(I think that they are not available in earlier versions):
1. ObservableCollection<T>[^]
2. BindingList<T>[^] - please don't use this for your purpose...


If you don't have .NET 4.0 framework available I suggest you to use some community solution:
1. Generic List (C#)[^]
2. The C5 Generic Collection Library[^]
3. Powercollections[^] - Not sure that they have "event aware" collection?

Or you can write you own collection types like this...
C#
public class YourList<T> : List<T>
    {

        public event System.EventHandler AddedItem;

        public new void Add(T item)
        {
            base.Add(item);
            if (AddedItem != null)
            {
                AddedItem(this, new EventArgs());
            }
        }
    }

This is only stub and you need to handle all other methods that are provided by List if you want to use this example...
Here is one more example[^] if you decide to implement it by yourself.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900