question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
weltkantecommented, May 8, 2020

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.

1reaction
weltkantecommented, Apr 20, 2020

Expected behavior: We should not add the duplicate. We should also sort the array

I was confused of whether this is asking for new behavior. To clarify for other readers, the described behavior are the invariants which Add and other methods already maintain, so the expected behavior is that the indexers setter maintains the same invariants as if it replacement were done via a Remove+Add pair.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found