none
How is my code changing my property through the 'get' attribute RRS feed

  • Question

  • To make a long story short I have the following code:

     private static ObservableCollection<Tool> _toolList = new ObservableCollection<Tool>();
    
            public static ObservableCollection<Tool> ToolList
            {
                get { return _toolList; }
               
            }

    As you can see I do not even have a 'Set" attribute in this property. 

    Yet I am able to add to this list with the following code

    ToolList.Add(mytool);

    My issue is that, somewhere in my code, something keeps changing my list and I can determine what it is.

    Nothing is using the _toolList anywhere. And When I track the code I notice that the code 'ToolList.Add() does not call the 'set' but instead works through the 'get'.

    I always thought that 'get' was to read the value from the property and 'set' was to set the value of the property. then how is my code changing the property using the 'get' attribute?

    Also, whatever is changing the list in the code, does so without even calling the 'get' . I just don't understand how the property can be changed without these things.

    Thursday, May 3, 2018 7:31 PM

Answers

  • What does your get method return? It is ObservableCollection object. This class has method Add. So if you return object which has Add method, everyone can add new item into collection. 

    Best solution is result IEnumerable<Tool>. IEnumerable has not Add method and nobody can add new item into collection. And then you can implement Add method in your class.

    This is about encapsulation princip. It means if you encapsulate Collection object you cannot expose it directly. Better solution is expose it through interface. It is same when I have List<string> in class and write property which will returns List<string>. Then everyone can call get property and add string into List. It is wrong because I expose private field directly through property. If I have collection I need expose collection items only. 

    private List<string> _list = new List<string>();
    
    public IEnumerable<string> Strings
    {
        get { return _list; }
    }
    
    public void AddString(string value)
    {
        _list.Add(value);
    }
    I return IEnumerable so nobody can Add new string value through property Strings because IEnumerable does not have Add method. If property would return List<string> there is Add method exposed because List has add method.

    • Edited by Petr B Thursday, May 3, 2018 7:55 PM
    • Proposed as answer by Ethan Strauss Thursday, May 3, 2018 7:56 PM
    • Marked as answer by PBPuddin Thursday, May 3, 2018 8:40 PM
    Thursday, May 3, 2018 7:50 PM

All replies

  • What does your get method return? It is ObservableCollection object. This class has method Add. So if you return object which has Add method, everyone can add new item into collection. 

    Best solution is result IEnumerable<Tool>. IEnumerable has not Add method and nobody can add new item into collection. And then you can implement Add method in your class.

    This is about encapsulation princip. It means if you encapsulate Collection object you cannot expose it directly. Better solution is expose it through interface. It is same when I have List<string> in class and write property which will returns List<string>. Then everyone can call get property and add string into List. It is wrong because I expose private field directly through property. If I have collection I need expose collection items only. 

    private List<string> _list = new List<string>();
    
    public IEnumerable<string> Strings
    {
        get { return _list; }
    }
    
    public void AddString(string value)
    {
        _list.Add(value);
    }
    I return IEnumerable so nobody can Add new string value through property Strings because IEnumerable does not have Add method. If property would return List<string> there is Add method exposed because List has add method.

    • Edited by Petr B Thursday, May 3, 2018 7:55 PM
    • Proposed as answer by Ethan Strauss Thursday, May 3, 2018 7:56 PM
    • Marked as answer by PBPuddin Thursday, May 3, 2018 8:40 PM
    Thursday, May 3, 2018 7:50 PM
  • I think I understand now.

    That did it for me. thanks

    Thursday, May 3, 2018 8:41 PM