dotnet thoughts 

a dotnet developer's technical blog

ObservableCollection Generic class

In .Net 3.0 Microsoft introduced a new type of Generic collection class in the Framework, it is called ObservableCollection. The advantage of this collection is that it will give notification based on the operations performed with the data in it. To work with this class you need to add a new assembly in the reference “WindowBase.dll”, normally it will not be available. This class is available in the Namespace “System.Collections.ObjectModel”. This class supports Add, Remove, Replace, Move and Reset. The details of the action and items will be available on the event “Specialized.NotifyCollectionChangedEventArgs” parameter.

Creating and Adding event

Dim _Collection As New System.Collections.ObjectModel.ObservableCollection(Of String)
AddHandler _Collection.CollectionChanged, AddressOf _Collection_CollectionChanged

In the event

Sub _Collection_CollectionChanged(ByVal sender As Object, ByVal e As
Specialized.NotifyCollectionChangedEventArgs)
        Select Case e.Action
            Case Specialized.NotifyCollectionChangedAction.Add
            Case Specialized.NotifyCollectionChangedAction.Move
            Case Specialized.NotifyCollectionChangedAction.Replace
                Console.WriteLine(e.Action.ToString)
                For Each item As String In e.NewItems
                    Console.WriteLine(e.Action.ToString & "-" & item)
                Next
            Case Specialized.NotifyCollectionChangedAction.Remove
                Console.WriteLine(e.Action.ToString)
                For Each item As String In e.OldItems
                    Console.WriteLine(e.Action.ToString & "-" & item)
                Next
		Case Specialized.NotifyCollectionChangedAction.Reset
                    Console.WriteLine("Collection reset")
        End Select
    End Sub

Here I am writing the output to the console based on the action, also the items affected. I made a simple implementation of ObservableCollection Generic class in .Net 2.0 over here. And you can get more information on ObservableCollection Generic class from MSDN

No Responses to “ObservableCollection Generic class”

RSS feed for comments on this post. TrackBack URL

Leave a Response

*