0

I have a list activeAcountList of object (ActiveAccount).

List<ActiveAccount> activeAcountList = new List<ActiveAccount>();

The object/ActiveAccount some props/info are Key, Name, Account, Amount, Source and some more properties.

I don't own this object, its been owned by a 3rd party and i am using an event given by them, that fires when ever something changes or added to the list. Below is the event method that get fires every time when something added or updated and i am using same event for populating my activeAcountList

Private void GetAccountList(ActiveAccount activeAccountData)
{
    if(!activeAcountList.Contains(activeAccountData.Key))
    {
        activeAcountList.Add(activeAccountData);
    }
    else
    {
        activeAcountList[activeAccountData.Key] = activeAccountData;
    }
}

Now my question is, is there any way i can attach an event that fires every time when 'Amount' changes? Or if i can keep eye on any property and when ever it changes this event fires? Just keep in mind i dont own this object so i can not do any thing with ActiveAccount class.

banker box
  • 137
  • 2
  • 3
  • 10
  • hmm which one class? ActiveAccount? If Yes, its not my class i dont have code for it. – banker box Oct 31 '15 at 00:44
  • So, `GetAccountList` is called when there is a change. I think you have a typo, why are you using `activeAccountData` as some kind of dictionary? I think you mean `activeAcountList`, right? Even with that, why are you changing the value in that list/dictionary? Do you get a different `ActiveAccount` instance when `GetAccountList` is invoked? if it is the same instance, you don't have to update the dictionary. – Yacoub Massad Oct 31 '15 at 01:34
  • @YacoubMassad Yes Yacoub , its like data might be updated some where else by some body else, so whenever data is updated i am updating my own list. And no its not a type its activeAccountData which is dictionary.. – banker box Oct 31 '15 at 01:56
  • This is confusing. Please review this line again: `activeAccountData[activeAccountData.Key] = activeAccountData`. Are you sure it is correct? – Yacoub Massad Oct 31 '15 at 02:01
  • Oops i didn't pay attention to else part you are right ma Bad.. its activeAcountList[activeAccountData.Key] = activeAccountData; – banker box Oct 31 '15 at 02:04

2 Answers2

0

This solution should work. Please check it Link

Community
  • 1
  • 1
Jigneshk
  • 350
  • 1
  • 7
  • i am already aware of ObservableCollection. Its not telling me What Property has been changed. i mean it will give me Whole Object with previous prop and Update Whole Object with new prop. I am not getting an Individual prop e.g; if Amount has been update i need to know Amount prop has been updated to this val... i hope it makes sense – banker box Oct 31 '15 at 02:59
0

System.ComponentModel.BindingList<T> can be one option. From MSDN

BindingList<T> have ListChanged event which fired when properties of item in the list are changed.
It fires also when items added or removed from the list

BindingList.ListChanged Event

Of course, for item's changes to be notified type of the item need to implement INotifyPropertyChanged interface

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • but i don't own the ActiveAccount class that contains each individual item so i can't implement INotifyPropertyChanged on ActiveAccount porperteis. Is there any way i can use INotifyPropertyChanged interface on list object items or something along these lines? – banker box Oct 31 '15 at 15:19
  • You can create your own "ViewModel" which implement `INotifyPropertyChanged` and use `ActiveAccount` instance as model. – Fabio Oct 31 '15 at 15:37