Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Framework entity
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:
 
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 19-Jan-12 2:30am
Edited 19-Jan-12 2:42am
v2
Comments
SAKryukov at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 5 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100