I have a loop running retrieving live share prices. What I want to do is check if any of the prices retrieved are different to the price I already have stored in a dictionary and notify me with details of all those that have changed. Was looking along the lines of Dictionary utilising INotifyCollectionChanged.
e.g.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace BasicExamples
{
class CollectionNotify:INotifyCollectionChanged
{
public Dictionary<string, string> NotifyDictionary{ get; set; }
public CollectionNotify()
{
NotifyDictionary = new Dictionary<string, string>();
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
public void Add(object k, object v)
{
NotifyDictionary.Add(k.ToString(),v.ToString());
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,0));
}
public void Update(object k, object v)
{
bool isUpdated = false;
IList<string> changedItems = new List<string>();
int index;
if (NotifyDictionary.ContainsKey(k.ToString()))
{
NotifyDictionary[k.ToString()] = v.ToString();
changedItems.Add(k+":"+v);
isUpdated = true;
}
else
{
Add(k, v);
}
if (isUpdated)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace,changedItems,changedItems));
}
}
}
}
with the main program calling Add/Update as a test. However when looping through the NotifyCollectionChangedEventArgs I have to nest the loop and cast to IEnumerable - this seems like a really long winded way round.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicExamples
{
class Program
{
//delegate
static void Main(string[] args)
{
//notify collection
CollectionNotify notify = new CollectionNotify();
notify.CollectionChanged += CollectionHasChanged;
notify.Add("Test1", "Test2");
notify.Add("Test2","Test2");
notify.Add("Test3", "Test3");
notify.Update("Test2", "Test1");
notify.Update("Test2", "Test3");
notify.Update("Test3", "Test1");
notify.Update("Test1", "Test3");
#region Lamba
//lamba
List<int> myList = new List<int>() {1,1,2,3,4,5,6};
List<int> newList = myList.FindAll(s =>
{
if (s == 1)
{
return true;
}
return false;
});
foreach (int b in newList)
{
Console.WriteLine(b.ToString());
}
#endregion
}
public static void CollectionHasChanged(object sender, EventArgs e)
{
NotifyCollectionChangedEventArgs args = (NotifyCollectionChangedEventArgs) e;
if (args.Action == NotifyCollectionChangedAction.Replace)
{
foreach (var item in args.NewItems)
{
foreach (var nextItem in (IEnumerable)item)
{
Console.WriteLine(nextItem);
}
}
}
}
}
}
I suppose my question is two fold:- A: Is this the best way to use a notifiable dictionary? B: Should i use the nested loop as above to retrieve the updated values?
Thanks in advance