user3356020
user3356020

Reputation: 119

regarding C# adding objects to list using foreach loop

foreach (string f in fileName)
{
    if (list.Where(p => p.FileName.Trim().Equals(f.Trim(), StringComparison.OrdinalIgnoreCase)).Count() == 0)
    {
        ServerpathID = GetSourceServerPath(projectID, out ServerPath);
        DellDirectory dir = new DellDirectory(ServerPath);
        lstgAFPFileInfo = GetFilesFromSourceServer(new string[] { f }, ServerpathID, SearchOption.TopDirectoryOnly).ToList();

        if (lstgAFPFileInfo.Count() != 0)
        {
            foreach (Service.GAFPFileInfo lstg in lstgAFPFileInfo)
            {
                projectfile.Checksum = string.Empty;
                projectfile.IsAutoDeleted = (autoDelete == true) ? true : false;
                projectfile.Size = lstgAFPFileInfo[0].Size;
                projectfile.IsArchived = false;
                projectfile.ProjectFileId = 0;
                projectfile.Language.LanguageId = 1;
                projectfile.LastModifyDate = lstgAFPFileInfo[0].LastModified;
                projectfile.ProjectPartLink = projectPartLink;
                projectfile.FileName = f;
                list.Add(projectfile);
            }
        }
    }
}

I have two files 1.txt and 2.txt in string[] filename. I am comparing these files with db and getting values into lstgAFPFileInfo. The first time it got the filename 1.txt and added to list. 2nd time it got the value 2.txt but after adding the file to the list it is overwrite the value 1.txt and again it added 2.txt. Now the list values are like this list[0]:2.txt and list[1]: 2.txt Can anyone help on this?

Upvotes: 3

Views: 5394

Answers (4)

Ryan Cox
Ryan Cox

Reputation: 958

It looks like you are creating a new string array foreach f in filename.

foreach (string f in fileName)
{
lstgAFPFileInfo = GetFilesFromSourceServer(new string[] { f }, ServerpathID, SearchOption.TopDirectoryOnly).ToList();

Thus, each iteration through the foreach, only the value of f at that time is created in the array. Try instantiating the array outside of your loop and then adding your value f inside.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

This is because your loop keeps adding the same object over and over, so your list ends up with multiple references to the same object.

Illustration

Add projectfile = new ProjectFile() to the top of your loop to fix this problem.

Upvotes: 7

D Stanley
D Stanley

Reputation: 152566

Becasue you're adding the same instance each time, just overwriting its properties. You need

projectfile = new WhateverClassNameProjectFileIs();

at the top of your innermost foreach loop.

Upvotes: 1

Chris
Chris

Reputation: 27609

You seem to be reusing the same object every time in projectfile. Even after it has been added to the list you have the same object being referenced in the list and in the variable so when you update its properties you update it in both places.

What you need is just to have a line at the beginning of your foreach saying something like:

projectfile = new ProjectFileObject();

This will create a new instance that is totally separate from the one already added to the list.

It should be noted that depending on what you have done with the projectfile object before a more complicated solution may be required but this highlights your basic problem.

Upvotes: 2

Related Questions