- Home /
Getting the sum of random values from Dictionary
Hello all!
Coding C# question here!
I have inventory with 4 different objects: Lemon, Apple, Coffee, Mango
each object has different score: 20, 40, 60, 80 (respectively).
I want to use dictionary to assign the values. Dictionary<string, float> scoreDictionary = new Dictionary<string, float>()
Now whenever I see only apple, Lemon and Mango (or other combination), I want the method to sum their values
So my plane is to add each value to dictionary and then be able to sum the values based on the Key I get.
if ((rows[0].stoppedSlot != rows[1].stoppedSlot) && (rows[1].stoppedSlot != rows[2].stoppedSlot)) { for (int i = 0; i < prizeDictionary.Count; i++) { // here summarize the three values I get } }
So basically, in other script I assign a string and add it's name + value to a dictionary,
and in the other script I want to say, "take the strings that you get" and sum the values based on the string name.
Answer by ray2yar · Dec 27, 2018 at 12:39 AM
So I take it you want to add the values of the other three items. Something like this might work, feed the function a string name, the loop goes through each key and compares, adds to the score IF the key doesn't match.
int FindSum (string thisItem)
{
int totalScore = 0;
foreach (string k in scoreDictionary.Keys)
{
if (k != thisItem)
{
totalScore += scoreDictionary[k];
}
}
return totalScore;
}
Your answer
Follow this Question
Related Questions
Getting the sum of random values in the dictionary 2 Answers
How to have Database of Dictionary elements with online images??? 0 Answers
Assigning a value to a Dictionary key changes other key values 0 Answers
How do I create a Key and Values Dictionary array in C# 1 Answer
How do I create a Key and Values Dictionary array in C# 1 Answer