Pregunta

I am iterating through a collection of KeyValuePair, then copying the Key and Value to a newly-created class as follows :

     Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
        List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();

        foreach (KeyValuePair<string, string[]> acct in acctData)
        {
            membHier.entityName = acct.Key;
            membHier.Accounts = acct.Value;
            membHierList.Add(membHier);                
        }

Problem is that upon the 2nd iteration, membHierList properties are immediately getting overwritten by the values in the first iteration. It's very strange.

So upon first iteration, membHier.entityName is "ABC Member" and the Accounts get populated with the string array no problem.

Then upon 2nd iteration, membHier.entityName is "XYZ Member".

Now "XYZ Member" occupies both slots as follows

membHierList[0].base.entityName = "XYZ Member" membHierList[1].base.entityName = "XYZ Member"

Do I have an object conflict up above ?

Thank you in advance.... Bob

¿Fue útil?

Solución

No it is not strange because you declare and initialize the object membHier just one time before the loop and then try to insert in the new list the same object.

This will resolve your problem

    List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();

    foreach (KeyValuePair<string, string[]> acct in acctData)
    {
        Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
        membHier.entityName = acct.Key;
        membHier.Accounts = acct.Value;
        membHierList.Add(membHier);                
    }

Declaring a NEW instance of the object MemberHierarchy at every loop will populate the list with different instances and every instance has its own values.
Instead, with the initialization outside the loop, at every iteration you update the same instance with the extracted values. But membHier is a reference type, so if not reinitialized, it points at the same memory where are stored the values of the first entity and you effectively overwrite these values with the second entity. At the end, all the elements in the list points to the same memory location and these memory locations contains the data of the last entity

Otros consejos

You need to move the construction of membHier to the inside of the foreach loop. The way your code is written now, only one instance of the MemberHierarchy class is created.

List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();

foreach (KeyValuePair<string, string[]> acct in acctData)
{
    Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
    membHier.entityName = acct.Key;
    membHier.Accounts = acct.Value;
    membHierList.Add(membHier);                
}

using current C# 4.0 syntax it would look like:

var membHierList = new List<Classes.MemberHierarchy>();

foreach (var acct in acctData)
{
    membHierList.Add(new Classes.MemberHierarchy
    {
       entityName = acct.Key;
       Accounts = acct.Value;
    });
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top