0

I have a list of objects:

List<NPortfolio> Portfolios = new List<NPortfolio>();
Portfolios.Add(new NPortfolio(1, "1", emptyPositions));
Portfolios.Add(new NPortfolio(2, "2", emptyPositions));

Now i want to call a Method on the object that modifies its properties:

Portfolios[0].UpdatePositions(db.GetPortfolio(1, Today));

The method is this:

public void UpdatePositions(Dictionary<string, double> valuepairs)
    {
        foreach (var k in this.positions.Keys.ToList())
        {
            if (valuepairs.ContainsKey(k))
                this.positions[k] = valuepairs[k];
        }
    }

This works, but the problem is that when I try to update just the first item of the list:

Portfolios[0].UpdatePositions(db.GetPortfolio(1, Today));

ALL ITEMS OF THE LIST ARE UPDATED!!! I cannot find why all items are updated and not only item 0. Please help this is really an headache

many thanks

class definition:

public class NPortfolio
{
    public string p_id { get; set; }
    public int p_nr { get; set; }
    private Dictionary<string, double> positions;
    public NPortfolio(int nr, string id, Dictionary<string, double> pos)
    {
        p_nr = nr;
        p_id = id;
        positions = pos;
    }

    public void UpdatePositions(Dictionary<string, double> valuepairs)
    {
        foreach (var k in this.positions.Keys.ToList())
        {
            if (valuepairs.ContainsKey(k))
                this.positions[k] = valuepairs[k];
        }
    }
    public Dictionary<string, double> getPositions()
    {
            return positions;
    }
}
Wesley Lomax
  • 2,067
  • 2
  • 20
  • 34
prre72
  • 697
  • 2
  • 12
  • 23
  • 2
    Can you provide the class definition for `NPortfolio`? – Joshua Shearer Oct 12 '15 at 14:09
  • 1
    Do you use a static variable for positions? – Domysee Oct 12 '15 at 14:13
  • Do they all share the same `Dictionary` instance? Also the `ContainsKey` makes no sense to me. – kevintjuh93 Oct 12 '15 at 14:14
  • 1
    How do you populate the list? Are you adding the same reference multiple times? – juharr Oct 12 '15 at 14:16
  • how do you fill the Portfolios collection ? what does db.GetPortfolio(1, Today) do ? Why do you do all your operations on 1 row ? - Portfolios[0].UpdatePositions(db.GetPortfolio(1, Today)); You should do it Nportfolio item = Portfolios[i]; then use item.UpdatePosition(...); – dlght Oct 12 '15 at 14:17
  • If I do it i 2 steps: Nportfolio item = Portfolios[i]; then use item.UpdatePosition(...); same issue, the entire list ist updated. – prre72 Oct 12 '15 at 14:18
  • 2
    You need to show us how you are populating the dictionary you are passing in to `pos` the problem is most likely there. – Scott Chamberlain Oct 12 '15 at 14:19
  • 1
    Don't put code in comments, delete those comments and update your question. – Scott Chamberlain Oct 12 '15 at 14:20
  • edit your post don't add it as a comment please – dlght Oct 12 '15 at 14:20
  • I'll expand on what @ScottChamberlain is saying by suggesting that you create a new dictionary inside you constructor and then populate it with the entries in the `pos` argument that is passed in. – juharr Oct 12 '15 at 14:23

2 Answers2

1

The problem is from this

Portfolios.Add(new NPortfolio(1, "1", emptyPositions));
Portfolios.Add(new NPortfolio(2, "2", emptyPositions));

You are passing the same dictionary to both classes, so if you modify one of the classes you modify both instances.

You must create a new dictionary inside the constructor of NPortfolio so each class has a unique copy.

public NPortfolio(int nr, string id, Dictionary<string, double> pos)
{
    p_nr = nr;
    p_id = id;
    positions = new Dictionary<string, double>(pos);
}

This will make a shallow copy of the dictionary and should solve your issue for now.

Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
0

You're passing the same dictionary into your objects. So when you update it in one you end up seeing the changes in the other. You should create a new dictionary inside your constructor and populate it with the values passed in.

public NPortfolio(int nr, string id, Dictionary<string, double> pos)
{
    p_nr = nr;
    p_id = id;
    positions = new Dictionary<string, double>(pos);
}
juharr
  • 31,741
  • 4
  • 58
  • 93