views:

70

answers:

1

Dear reader,

I'm having some trouble explaining the question in a single line for the title, but hopefully this description will provide you with enough insight to fully understand my question:

I have a few variables in a Word document. Every variable has a Value which is a number (starting from 0 up to the number of variables). A SortedDictionary is being filled out with these variables, but it's possible that a variable has been deleted before, so there's a 'gap' so to say. Example:

5 Variables are being added to a SortedDictionary<int, string> where the first number is the int and the string corresponds to the string part of the SortedDictionary.

0 "name 1"
1 "name 2"
2 "name 3"

Now one of the variables is deleted so the dictionary is filled out like this:

0 "name 1"
2 "name 3"

Eventually all of the SortedDictionary entries are added into a list box and I'm using the first number as the index for the insertion. You can imagine that it will give an error when it tries to add an item into index 2 when index 1 doesn't exist, but index 0 does.

The way I want to solve it is when the user calls the DeleteVariable() method and a variable is taken away, all the variables should automatically update their value (number) so when the SortedDictionary gets all the variables the list box no longer gives an error because all the numbers match (no gaps).

Please advise.

+7  A: 

I would use a List instead of the sorted dictionary. Your number will be the index in the list. This way if you remove an item from the list (using probably list.RemoveAt(index)), all the other indices will be effectively "updated" in a way you wanted.

Grzenio
Yes, my thought also (but I made a mistake in my answer so I'll vote for yours instead).
ho1
That's perfect. Then I can just adjust all the variable values based on the indices of the list. Brilliant, thank you!
Kevin van Zanten