Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

SortedList RemoveAt doesn't work

I've got a sorted list mySortedList. It has the following information:

Entry0
Entry1
Entry2
Entry3
Entry4

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?




0
diangysystems
Asked:
diangysystems
  • 3
1 Solution
 
S-TwilleyCommented:
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
0
 
S-TwilleyCommented:
I've just tried:

        ListBox1.Items.RemoveAt(0)
        MsgBox(ListBox1.Items(0))

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

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

When you say
mySortedList.Item(0)
It looks for an Entry in the List with a KEY value of 0

When you say
mySortedList.RemoveAt(0)
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....
0
 
S-TwilleyCommented:
ahhh... i misread (or took it a different way)... and thought you meant a sorted listbox ... oops! :P
0
 
diangysystemsAuthor Commented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now