Reputation: 886
When I add information to a BindingList, it gets duplicated... I dont get how...
I have this class with the Lists:
public VideoRepository()
{
videos = new BindingList<Video>();
videosFiltered = new BindingList<Video>();
}
public BindingList<Video> videos { get; set; }
public BindingList<Video> videosFiltered { get; set; }
public void addVideo(Video video)
{
Console.WriteLine("Size 1 " + videos.Count);
videos.Add(video);
videosFiltered.Add(video);
Console.WriteLine("Size 2 " + videos.Count);
}
When I call the method addVideo
the first print shows Size 1 0
and the second print shows Size 2 2
. Even when using the debuger...
What is the problem? Am I very drunk?
Upvotes: 1
Views: 245
Reputation: 205719
I've noticed that both your videos
and videosFiltered
have public setters. The only way you can get the behavior described is if some external code (not shown here) sets them to one and the same BindingList<Video>
instance.
You'd better remove the public setters.
Or, modify the code as follows
public void addVideo(Video video)
{
Console.WriteLine("Size 1 " + videos.Count);
videos.Add(video);
if (videosFiltered != videos)
videosFiltered.Add(video);
Console.WriteLine("Size 2 " + videos.Count);
}
Upvotes: 1