Zoma
Zoma

Reputation: 329

Updating the value of a dictionary which is itself the value of another dictionary

I'm working on an application where I want to use a Dictionary in another one (I may have done this with a custom object, but I never used Dictionary so I want to give it a try). I actually got a Dictionary like this :

Dictionary<string, Dictionary<string, double>>

Later, I'll refer to the first key as "reference", to the second as "name" and to the double as "quantity".
Here's what I want to do with this :

if the first Dictionary contain key reference
    if the value of key reference contain the key name
        add to the value of key name quantity
    if there isn't
        add to the value of key reference a Dictionary which as name as key and quantity as value
if there isn't
    add reference as a key and as value a second Dictionary which as name as key and quantity as value

Here's what I actually have :

Dictionary<string, Dictionary<string, double>> firstDictionary = new Dictionary<string, Dictionary<string, double>>();
Dictionary<string, double> buffer = new Dictionary<string, double>();

string reference;
string name;
double quantity;

SQLDataReader reader = myQuery.ExecuteReader();

while(reader.Read())
{
    reference = reader["reference"].ToString();
    name = reader["name"].ToString();
    quantity = Convert.ToDouble(reader["quantity"].ToString().Replace(".", ","));

    if (firstDictionary.ContainsKey(reference))
    {
        if(firstDictionary[reference].ContainsKey(name))
        {
            firstDictionary[reference][name] += quantity;
        }
        else 
        {
            firstDictionary[reference][name] = quantity;
        }
    }
    else
    {
        buffer.Clear();
        buffer.Add(name, quantity);
        firstDictionary[reference] = buffer;                            
    }
}

But I got a problem with this. My query return is what I expect so there's no problem with that, but my Dictionary isn't.
All references are like they should, but every names and quantities are the same. It looks like all name are changed when I add a new one and all quantity are set to the same value when I try to update one. Given that, I tried to change some few things, but nothing changed.

I guess it's because of the Dictionary in another Dictionary so :
Is it possible to do want I want to with Dictionary ? (I know it's not the best solution anyway)
If so, could anyone help me out with this ? Even just a hint would be appreciated here.

Upvotes: 2

Views: 84

Answers (1)

Matt Burland
Matt Burland

Reputation: 45135

The problem is in your last else clause:

else
{
    buffer.Clear();
    buffer.Add(name, quantity);
    listeRef[reference] = buffer;                            
}

What you are doing here is clearing your buffer and then adding a new value and then adding it to listeRef (which I'm assuming is a typo and is supposed to be firstDictionary). But what you are doing here is adding the same dictionary for every value of reference. When you assign it to firstDictionary you aren't making a copy, you are storing a reference. And when you clear the buffer you are clearing the object that every entry in firstDictionary is pointing to. That's why you see the same value (presumably the last one processed) in every key.

So let's say you start with an empty firstDictionary and the first value is foo, bar, 10 - you end up with a firstDictionary that might look something like this:

{
    "foo" : {
        "bar": 10
    }
}

Now the next value is la, far, 12. What happen is you clear buffer, and you end up with:

{
    "foo": {}
}

Then you add the new value to buffer and you get:

{
    "foo" : {
        "far": 12
    }
}

and then you add you "new" (but it's actually the same) dictionary under the new key to firstDictonary:

{
    "foo" : {
        "far": 12
    },
    "la": {
        "far": 12
    }
}

You need to create a new dictionary in that else clause:

firstDictionary[reference] = new Dictionary<string,double>() { { name, quantity }};

Upvotes: 2

Related Questions