• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

insert a keyvaluePair in middle of dictionnary ?

Hello EE,
I have a dictionnary of integer, string


lets say :

1 , Earth
2, Hi
3, Hello Mona Lisa
4, Moon


lets say I do :

for each kvp as KeyValuePair(Of Integer, String) in dict
  if kvp.Value.Contains("Hello") then
         ---> here is my problem:   insert new KVP AFTER index 3 and put : 4, Hello Peter
  end if
next

so then 4,Moon would now be 5,Moon and so forth


is that possible ??
0
PhilippeRenaud
Asked:
PhilippeRenaud
2 Solutions
 
mcmahon_sCommented:
Not without renumbering everything after the value you are trying to insert (i.e. removing and reinserting with a new key). But..... if this is really what you need then that more than likely means you have a poor implementation choice because your key once assigned should not change.

What is the purpose of what you are trying to do?
0
 
käµfm³d 👽Commented:
Agreed. Why not use a List(Of String) and use the implicit numbering you get with the index of the particular item (offsetting for zero, of course!)?
0
 
PhilippeRenaudAuthor Commented:
you're right. I should not use Dictionnary for my purpose.
list might be easier.

i was thinking that right afgter I press "submit your question" .. :P
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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