ListBox.IntegerCollection should contain unique values, but if we use the item setter, we can have duplicated
See original GitHub issue-
.NET Core Version: Master
-
Have you experienced this same bug with .NET Framework?: Yes
Problem description:
ListBox.IntegerCollection should be a collection of unique integers. However, if we call the collection[index] = valueAlreadyInCollection we can end up with duplicates - see repro
Expected behavior:
We should not add the duplicate. We should also sort the array
Minimal repro:
Notice that the collection contains duplicates and is not sorted
[WinFormsFact]
public void ListBoxIntegerCollection_IListItem_Set_ReturnsExpected()
{
using var owner = new ListBox();
IList collection = new ListBox.IntegerCollection(owner);
collection.Add(2);
collection.Add(1);
collection.Add(1);
collection.Add(3);
// Set first.
collection[0] = 4;
Assert.Equal(new int[] { 4, 2, 3 }, collection.Cast<int>());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);
// Set middle.
collection[1] = 1;
Assert.Equal(new int[] { 4, 1, 3 }, collection.Cast<int>());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);
// Set last.
collection[2] = 4;
Assert.Equal(new int[] { 4, 1, 4 }, collection.Cast<int>());
Assert.Empty(owner.CustomTabOffsets);
Assert.False(owner.IsHandleCreated);
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
List box duplicated values
Solved: Hi All, I am learning Qlikview. I don't know why my list box have duplicated it's values when I have the field...
Read more >VBA - Excel Listbox - Look for Duplicates when Adding ...
The problem is that you can add a specific item more than once, and considering I would like to use the values in...
Read more >How to prevent duplicate entries in SharePoint lists and ...
With this post, I will explain a little-known functionality called “enforce unique values” and how you can use it to prevent duplicate entries ......
Read more >How to fill listbox on Excel user-form with unique values from ...
Now you assign the same value to the key and the item. So no duplicate values can be added. Every time VBA encounters...
Read more >Solved: Distinct function for combobox is populating blank...
Solved: Hey guys, im trying to do a simple thing - I have a SP List in which the "Title" column have some...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes thats what some other sorted collections do. I don’t have a strong opinion over doing one or the other, just wanted to raise awareness.
indexer-setter is kind of like a “ReplaceAt” operation, so you can also argue for just supporting indexer-setter and still throwing in Insert. Its mostly a thing of proper documentation I think.
The important thing is that no method should break the collection invariants of being sorted and duplicate free, so either throw or keep the invariants intact.
I was confused of whether this is asking for new behavior. To clarify for other readers, the described behavior are the invariants which
Addand other methods already maintain, so the expected behavior is that the indexers setter maintains the same invariants as if it replacement were done via aRemove+Addpair.