I'll try to be as brief as possible.
I have a class named Error
. This class contains a property named Suggestions
which is an ObservableCollection<string>
.
In one method, I create a list of Error
s named AllInstances
with a LINQ statement and then do a foreach
loop on that list. Inside the loop, I add one item to the Suggestions
collection of the Error
object.
The problem is that on each turn of the loop, the item is added to ALL of the members of the AllInstances
. Therefore, if the loop has 3 turns, then every member of the AllInstances
will have 3 items added to them!
The normal behavior should be that each Suggestions
collection have 1 item added to it, not 3.
Here's the code:
Error Class
public class Error : INotifyPropertyChanged, ICloneable
{
public ObservableCollection<string> Suggestions { get; set; }
}
The Method's Code
List<Error> AllInstances = (from paragraph in DocRef.LatestState
from error in paragraph.Errors
where error.Word == sourceError.Word
select error).ToList();
foreach (Error _er in AllInstances)
{
_er.Suggestions.Add(newSuggestion);
}
// At this point, if "AllInstances.Count" was 3, every "_er.Suggestions" in "AllInstances"
// will also have 3 items in it!
This seems so illogical to me that I highly suspect it's something that I've done or something basic about the structure of types that I don't yet know.
I appreciate any help. Thank you.