Dictionary - how modify an entry

I would like tu update an entry in a dictionary, but I have problem to find a solution.
I don’t understood way therei is not a system funtion ready to use…

BTW I have create a routine to seatch and modify an entry, but do not work, loop indefinitely.

this is the code:

Function DictUpdateValue(aDict as Dictionary, theKey as Variant, theNewValue as Variant) As int
eger
//Given an Dictionary aDict
// search for key thekey
// update the correspnding value using thenewValue
For Each entry As DictionaryEntry In aDict
If entry.Key.Equals(theKey) Then
aDict.Value(theKey) = theNewValue
End If
Next
Return 0
End Function

Any help is quite appreciate

Function DictUpdateValue(aDict As Dictionary, theKey As Variant, theNewValue As Variant) As Integer
  ' Check for existing key
  If aDict.HasKey(theKey) Then
    ' Key found, update value
    aDict.Value(theKey) = theNewValue
  End If
End Function
1 Like
If aDict.HasKey(theKey) Then
    aDict.Value(theKey)=theNewValue
End

Even

aDict.Value(theKey)=theNewValue

on its own is probably good enough.
If a key of that value exists, it is amended.
If it does not exist, it is created.

You don’t even really need this DictUpdateValue function, unless the Return 0 is useful to you?

5 Likes

I have investigate…
and found that the key was not build exactly the same in the build time and in the search/update time, the different was a space at the end
Solved!
Thank you