Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix 'Remove property setter' build error?

Tags:

c#

asp.net-mvc

I have a property in a model which has auto property getter and setter:

[DataMember]
public Collection<DTOObjects> CollectionName { get; set; }

I get the following error when building the solution:

Microsoft.Usage : Change 'propertyname' to be read-only by removing the property setter.

However, when I remove the setter and run the code, an error occurs because it's trying to set the property! It appears it's asking me to remove the setter despite the fact it is being set somewhere in the code.

Has anyone else come accross this problem? What do I need to modify?

like image 903
Theomax Avatar asked Oct 26 '12 08:10

Theomax


1 Answers

I'm going to guess this is a list/collection (or something similar), in which case yes - it is unusual to have a setter. A typical example might be:

private readonly List<Foo> items = new List<Foo>();
public List<Foo> Items { get { return items; } }

Most callers should not be trying to assign to that; they shouldn't need to - they can add/remove/enumerate/clear/etc the list without ever needing to assign it.

an error occurs because it's trying to set the property

Then consider changing that code so that it doesn't try to set the property. It should not need to in virtually all cases.

like image 147
Marc Gravell Avatar answered Nov 03 '22 22:11

Marc Gravell