Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to implement undo function in my winform project.
i have implemented a stack which store property value and property name as object.
when i need to undo i just pop from stack and reassign the value. now the problem is its working fine with string property, but not with collection. i.e. i have a class i.e. class x, which contain some property i.e. String ID etc and some other collection i.e. List<y> , List<z> and these collection classes also have some collection i.e. Class Y have "List<a>" etc

C#
class X : inotify
{
   string id;
   YCollection YCol;
   ZCollection ZCol;
}

YCollection : List<y>
{
   Add(Y)
{
---
}
Remove(Y)
{
}

}

Class Y: inotify
{
   string ID;
   ACollection ACol;
}

Class ACollection : List<a>
{
   Add(A)
   Remove(A)
}

class A : inotify
{
   string ID;
}

now i dont know how to tackle this problem, because when change occure in lower level i.e. Class A object, i dont know how to find where change occure, and when to push this into stack for undo.
can someone help.
Posted
Updated 11-Jun-12 18:46pm
v2

1 solution

Hello

Use ObservableCollection instead of List


For example:

C#
private ObservableCollection<MyClass> _myProperty;
public ObservableCollection<MyClass> MyProperty
{
   get { return _myProperty; }
   set
   {
                _myProperty = value;
                _myProperty.CollectionChanged += new NotifyCollectionChangedEventHandler((object sender, NotifyCollectionChangedEventArgs e) => {
   //Some Code
   }
}



http://msdn.microsoft.com/en-us/library/ms668604.aspx[^]
http://msdn.microsoft.com/en-us/library/ms653375.aspx[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 12-Jun-12 1:42am    
I would note that your question is not quite correct then. If you cannot make a decision, thing about it: by asking such question you simply waste expert's time. If your "senior" tells you what to do, let him ask the question; or this person should make sure you can do the job. Why should we get involved in the problem of your relationships with your colleagues? Please keep such excuses to yourself and regulate your communications in your team by yourself. If you ask a question, be ready to use it. Or present your "constraints" in first place, but don't expect them to be welcome. If you asking how to achieve some behavior, don't tell use "don't use this and that"...
--SA
Shahin Khorshidnia 12-Jun-12 2:15am    
%100 Agreed.
jp1981 12-Jun-12 2:27am    
sorry, but it was not an excuse. it was my mistake that i didn't put this while asking question. i will make sure that i will not do this again in future. i am already working on the solution suggested by shahin.
Shahin Khorshidnia 12-Jun-12 2:21am    
Jitendra, I don't think List can notify while it's updating. You should use another kind of collection and ObservableCollection is recommended.
jp1981 12-Jun-12 2:24am    
Thanks a lot Shahin. i am working on solution suggested by you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900