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)}}
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.
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)}}
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
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")))))