How to get and update the value of a dynamic key in a dictionary

Hi People,

I declared a dictionary, newDict = New Dictionary(Of String, Decimal), however i intend to add random peoples names as string and a decimal value to the dictionary, i also want to ensure that names aren’t repeated and for the repeated names, whatever decimal value i pass should be added to the value in the dictionary. For instance

New Dictionary {“James Smith”, 7.8; “Luke Jake”, 12.7; “Brain Smart”, 8.9}
First i checked New Dictionary.containsKey(persons name).
Now i need to get the value of the name if the key exists. How do i do that?
i tried this Decimal.Parse(New Dictionary.GetType().GetProperty(persons name).GetValue(New Dictionary).ToString) + 3.4 . but i keep getting the error -Add to dictionary : Object reference not set to an instance of an object.

Thank you

(Replying to earlier posted solutions)seems to be quite different from the replies i got. So basically i have this array of object [
{ name : “James”
value: 34
},
{name : “James”
value: 78
},
{name: “gladys”
value:67
}
{name: “gladys”
value:86
}
]
i want to create a dictionary, new dictionary = {{James, (34+78)},{gladys, (67+86)}}

Hi @Olaoluwa,

add%20to%20dict

update value in the dictionary
myDictionary[myKey] = myNewValue;

@Olaoluwa

First take a list os strings List A

ListA=(DictionaryVariable.Keys).ToList
Now Check in if condition ListA.Contains(“Person Name”)
If it contains decimal a=Dictionary Variable(“Person Name”)
Else you can add the value to the Dictionary.

Regards,
Mahesh

seems to be quite different from what i want to achieve. So basically i have this array of object [
{ name : “James”
value: 34
},
{name : “James”
value: 78
},
{name: “gladys”
value:67
}
{name: “gladys”
value:86
}
]
i want to create a dictionary, new dictionary = {{James, (34+78)},{gladys, (67+86)}}

@Olaoluwa
Firstly you have some validation errors in Json Format

Modify like this
[
{ “name” : “James”,
“value”: 34
},
{“name” : “James”,
“value”: 78
},
{“name”: “gladys”,
“value”:67
},
{“name”: “gladys”,
“value”:86
}
]

Then Copy the format in notepad and save it.
Then Read the text file and store it in a string stra.

Then Parse the string to Json Array by using below ,it should be assigned to a variable of type JArray
Newtonsoft.Json.Linq.JArray.Parse(stra)
Then Create a dictionary of type (of string, string) Dictionary

Then use the below query

Dictinary=(From p In jobject
Group p By Name= p("name").ToString Into GroupA=Group
Select GroupA).ToDictionary(Function(x)  x(0)("name").ToString, Function(x) Convert.ToString(x.Sum(Function(y) Convert.ToDouble(y("value")))))

Now you can acces the Key Value pairs.
Dictinary(“James”) will give 112

Regards,
Mahesh

1 Like

Thank you Manesh… i cant seem to see the full picture of the query you sent. Can you please resend?

Dictinary=(From p In jobject
Group p By Name= p("name").ToString Into GroupA=Group
Select GroupA).ToDictionary(Function(x)  x(0)("name").ToString, Function(x) Convert.ToString(x.Sum(Function(y) Convert.ToDouble(y("value")))))
1 Like

@Olaoluwa

Can you able to see now

Regards,
Mahesh

yes i can see it… im geting the error option strict on disallows late binding

it works now thank you

1 Like