Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object has changed?

Tags:

c#

I have an object, which contains other objects, which contain other objects, including lists, etcetera. This object is databound to a form, exposing numerous fields to the user in different tabs. I also use master-child datagridviews.

Any idea how to check if anything has changed in this object with respect to an earlier moment? Without (manually) adding a changed variable, which is set to true in all (>100) set methods.

like image 938
willem Avatar asked Oct 31 '11 10:10

willem


People also ask

How do you check object is changed or not in Javascript?

Method 2: Object.prototype.watch() The watch function includes a handler which has access to the old value and the new value of the watched property of the object. obj. watch(“foo”, function (oldValue, newValue) {console. log(`Value of foo changed from ${oldValue} to ${newValue}`);});

How do you know if an object has the same value?

To check if all of the values in an object are equal, use the Object. values() method to get an array of the object's values and convert the array to a Set . If the Set contains a single element, then all of the values in the object are equal.

How do you check if an object has properties?

The hasOwnProperty() method will check if an object contains a direct property and will return true or false if it exists or not. The hasOwnProperty() method will only return true for direct properties and not inherited properties from the prototype chain.

How do you compare objects in Javascript?

Comparing objects is easy, use === or Object.is(). This function returns true if they have the same reference and false if they do not. Again, let me stress, it is comparing the references to the objects, not the keys and values of the objects. So, from Example 3, Object.is(obj1,obj2); would return false.


2 Answers

As Sll stated, an dirty interface is definitely a good way to go. Taking it further, we want collections to be dirty, but we don't want to necessarily set ALL child objects as dirty. What we can do, however is combine the results of their dirty state, with our own dirty state. Because we're using interfaces, we're leaving it up to the objects to determine whether they are dirty or not.

My solution won't tell you what is dirty, just that the state of any object at any time is dirty or not.

public interface IDirty
{
    bool IsDirty { get; }
}   // eo interface IDirty


public class SomeObject : IDirty
{
    private string name_;
    private bool dirty_;

    public string Name
    {
        get { return name_; }
        set { name_ = value; dirty_ = true; }
    }
    public bool IsDirty { get { return dirty_; } }
}   // eo class SomeObject


public class SomeObjectWithChildren : IDirty
{
    private int averageGrades_;
    private bool dirty_;
    private List<IDirty> children_ = new List<IDirty>();

    public bool IsDirty
    {
        get
        {
            bool ret = dirty_;
            foreach (IDirty child in children_)
                dirty_ |= child.IsDirty;
            return ret;
        }
    }

}   // eo class SomeObjectWithChildren
like image 116
Moo-Juice Avatar answered Oct 17 '22 05:10

Moo-Juice


You can implement INotifyPropertyChanged interface and if you user VS2010 there is addin that automatic alter all properties in IL (so you don't have to implement it manualy).

I belive there is also some other methods that use Weaving technique.

I found addin in vs2010 gallery:

http://visualstudiogallery.msdn.microsoft.com/bd351303-db8c-4771-9b22-5e51524fccd3

There is nice example - your code:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }
}

What get compiled:

public class Person : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private string givenNames;
    public string GivenNames
    {
        get { return givenNames; }
        set
        {
            if (value != givenNames)
            {
                givenNames = value;
                OnPropertyChanged("GivenNames");
                OnPropertyChanged("FullName");
            }
        }
    }
}

This is from first resoult from unce G (might be usefull):

http://justinangel.net/AutomagicallyImplementingINotifyPropertyChanged

http://www.codeproject.com/KB/WPF/AutonotifyPropertyChange.aspx

like image 37
Kamil Lach Avatar answered Oct 17 '22 06:10

Kamil Lach