0

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.

ron
  • 35
  • 1
  • 7
  • 2
    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 – Nkosi Nov 04 '16 at 12:47
  • 2
    I believe (but not sure hence comment not answer) that it's because you're not amending the collection, you're pointing the variable to a new place. – Andy Nichols Nov 04 '16 at 12:47
  • 3
    Probably worth reading up on reference objects – Jonesopolis Nov 04 '16 at 12:52
  • Hi @Nkosi, How will I assign the values in strList2 to strList? – ron Nov 04 '16 at 12:52
  • ron, why don't you ask what you are trying to achieve – Pikoh Nov 04 '16 at 12:54
  • @Pikoh Op stated `Basically, I want to check if I already assigned new values to my ObservableCollection.` – Nkosi Nov 04 '16 at 12:55
  • @Nkosi yeah, but now is asking how to asign values in strList2 to strList, so it smells as an XY problem – Pikoh Nov 04 '16 at 12:56
  • @Pikoh I see your point. agreed. – Nkosi Nov 04 '16 at 12:57
  • Hi Guys, Thanks for the answers. Yeah seems like I understand now what you guys trying to explain to me. I read up about reference object and resolved the issue. Thanks a lot – ron Nov 04 '16 at 13:00
  • @ron check answer – Nkosi Nov 04 '16 at 13:02

3 Answers3

3

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.

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

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
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

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...
Fab
  • 14,327
  • 5
  • 49
  • 68