ron
ron

Reputation: 35

C# - CollectionChange doesn't detect change in values

I am new in C# and want to detect if my List was changed. Based on my research I should use ObservableCollection instead of List. Basically, I want to check if I already assigned new values to my ObservableCollection.

Here is a sample code similar to mine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
namespace Rextester
{
    public class Program
    {
       public static void Main(string[] args)
       {
            ObservableCollection<string> strList = new ObservableCollection<string>{"1", "2"};
            strList.CollectionChanged += onCollectionChange;
            //strList.Clear();
            ObservableCollection<string> strList2 = new ObservableCollection<string>{"1", "2", "3"};
            strList = strList2;
        }

       public static void onCollectionChange(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){
            Console.WriteLine("Changed");
        }
    }
}

It should print "Changed" when I assign strList2 to strList right? I noticed that when I un comment the strList.Clear();, "Changed" will not be printed. What am I doing wrong here? Please help. I tested the codes at http://rextester.com/

Thanks in advance.

Upvotes: 0

Views: 165

Answers (3)

Fab
Fab

Reputation: 14813

MSDN is your friend

By the way, google also is but on this particular point, if you're going to learn about .Net Framework or Microsoft technologies in general, you should read the related articles on MSDN.

Here, there is clearly a basic misunderstanding on how collections are working and how variables are working. So you tried to tackle ObservableCollection before understanding C# in general and Collections in particular.

The link on Collection have some code sample to explain how they are working.

In your code, you don't change the ObservableCollection pointed by the variable strList, you just overwrite the reference (a pointer if you prefer, which contains the address of the object) stored in the variable strList by a reference to another instance of ObservableCollection: strList2.

The following will change the collection:

strList.Add(newItem);
strList.Remove(oldItem);
strList.Clear();
// etc...

Upvotes: 0

Nkosi
Nkosi

Reputation: 247551

No. you basically cancelled the event handler when you re assigned the variable strList. The event is fired when you modify the collection itself. ie Add, Remove, clear

Basically, I want to check if I already assigned new values to my ObservableCollection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
namespace Rextester
{
    public class Program
    {
       public static void Main(string[] args)
       {
            ObservableCollection<string> strList = new ObservableCollection<string>{"1", "2"};
            strList.CollectionChanged += onCollectionChange;
            //strList.Clear();
            var strList2 = new ObservableCollection<string>{"1", "2", "3"};
            Merge(strList,strList2);
        }

        public static void Merge<T>(ICollection<T> a, ICollection<T> b) {
            var diff = b.Except(a).ToList();
            diff.ForEach(a.Add);
        }

       public static void onCollectionChange(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e){
            Console.WriteLine("Changed");
        }
    }
}

Output:

Changed

Upvotes: 0

RvdK
RvdK

Reputation: 19800

strList Initially points to a ObservableCollection to which you hook up the CollectionChanged event.

After that, you let point strList to a new ObservableCollection. So you don't change the collection, you just don't use it anymore.

Upvotes: 3

Related Questions