views:

36

answers:

1

Hello,

I am using wpf and its C sharp!

I have this in my Animal.cs clas

private string _animalName;

    public string AnimalName
    {
        get { return _animalName; }
        set
        {
            if(_animalName!= value)
            {
                _animalName= value;
                this.NotifyPropertyChanged("AnimalName");
            }
        }
    }

I could also write:

public string AnimalName {get;set;}

There is no difference in binding and validation. Everythings works as before when I exchange the code.

Is this due to the fact that I only create new animals but I do not allow to update the animals name in my application ?

So I need to call the propertyChanged("AnimalName"); only when I want to change its property value?

I am a c# beginner ;)

+1  A: 

If your object has an updateable property (setter) that will be bound to a control then you need to ensure to let the bound control know of any changes to that property via INotifyPropertyChanged. However, if you have a readonly property and/or a property that's not going to be used in a data-binding scenario then you don't care about implementing or calling NotifyPropertyChanged method from within that property's setter in which case you can use automatic properties.

Mehmet Aras
hm well I have a setter because the first time I have to enter an animals name, when I create the Animal class. But after that process I do not have to change the property anymore forever!I just switched the properties and it works without NotifyPropertyChanged although I use a setter as you say, so what?
Honey
If the animal's name is not going to change once an instance of an animal class is created, you can have animal constructor take a parameter for the name. You set the name in the constructor and make the name property readonly by removing the setter. It will work regardless of NotifyPropertyChanged. But say you have a property for the owner of an animal which can change. Binding to and displaying owner information will still work without NotifyProperty. However, if the owner changes, you will not be displaying the most up-to-date information without NotifyPropertyChanged.
Mehmet Aras
Do you speak of a Owner.cs and Animal.cs relation?What I do not understand when I read what you say and same time I regard whats happening on my side.I use automatic props only with get;set and everything works as expected. I just do not want to have some drawbacks later in some months...
Honey