SortedList RemoveAt doesn't work

Posted on 2005-04-06
Last Modified: 2010-04-24
I've got a sorted list mySortedList. It has the following information:


I call mySortedList.RemoveAt(0). When I try to access mySortedList.Tem(0), I get a nothing reference error. According to the documentation after I call mySortedList.RemoveAt(0), my list should look like this:
Entry0 (formerly Entry1)
Entry1 (formerly Entry2)
Entry2 (formerly Entry3)
Entry3 (formerly Entry4)

Am I misreading the documentation?

Question by:diangysystems
    LVL 12

    Expert Comment

    could you post up the code involved to see if there is a slipup somewhere... and also so we can try to get the error you're getting
    LVL 12

    Expert Comment

    I've just tried:


    on a sorted list... and lines in that quick succession work fine... so if you can post it up, we'll have a look
    LVL 19

    Accepted Solution


    Hi diangysystems
    You have read it right (Well Almost)
    But you are, as it appears, mixing "KEY" and "INDEX"

    When you say
    It looks for an Entry in the List with a KEY value of 0

    When you say
    It Removes the Item at INDEX 0
    so naturally after this the Next Item in the List is Promoted (as the documentation says)

    I just prepared a small Example that will help you understand it...

            Dim SL As New SortedList
            SL.Add("Item0", "Item Zero") 'Key is Item0, And Index is 0
            SL.Add("Item1", "Item One") 'Key is Item1, And Index is 1
            SL.Add("Item2", "Item Two")
            SL.Add("Item3", "Item Three")
            MsgBox(SL.Item("Item0")) 'We are looking for an Entry whose Key is the Text Item0
            MsgBox(SL.GetByIndex(0)) 'We are looking for the Entry at Position (INDEX) 0
    'Now Here both will return "Item Zero" as the value with Key Item0 is at position 0

            SL.RemoveAt(0)    'Now we remove the Item at Position 0
            MsgBox(SL.GetByIndex(0))  'As the documentation says, now "Item One" has been moved up so this returns "Item One"
            MsgBox(SL.Item("Item0")) 'But this is Wrong as we have Deleted the Item with Key "Item0"

    Hope you got it....
    LVL 12

    Expert Comment

    ahhh... i misread (or took it a different way)... and thought you meant a sorted listbox ... oops! :P

    Author Comment

    Yes, I was talking about a SortedList, not a Listbox. Arif_eqbal, I'll test it out, but thanks for the clarification. It makes more sense now.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now