1

I have a problem with elements in my list.

My code:

List<Rack> racksUKL;
List<Rack> racksUKLDraft;

var result = xlsxToList.NewListUKL(textBoxSequenceExecution.Text);

racksUKL = result.Item1;

racksUKL = racksUKL.OrderBy(x => x.Date).ToList();

racksUKLDraft = racksUKL;

Debug.WriteLine(racksUKL.Count());

for (int i = 0; i < racksUKLDraft.Count - 2; i += 2)
{
    for (int j = 0; j < racksListMatrix.Count - 1; j++)
    {
        if (racksUKLDraft[i + 1].TruckNo == racksListMatrix[j].TruckNo
            && racksUKLDraft[i].TruckNo == racksListMatrix[j + 1].TruckNo
            && racksUKLDraft[i + 1].TruckPosition == racksListMatrix[j].TruckPosition
            && racksUKLDraft[i].TruckPosition == racksListMatrix[j + 1].TruckPosition
            || racksUKLDraft[i + 1].TruckNo == racksListMatrix[j].TruckNo2
            && racksUKLDraft[i].TruckNo == racksListMatrix[j + 1].TruckNo2
            && racksUKLDraft[i + 1].TruckPosition == racksListMatrix[j].TruckPosition
            && racksUKLDraft[i].TruckPosition == racksListMatrix[j + 1].TruckPosition)
        {
            markUKL(racksUKLDraft[i].Id);
            markUKL(racksUKLDraft[i + 1].Id);

            racksUKLDraft.RemoveAt(i);
            racksUKLDraft.RemoveAt(i);

            if (i >2)
            {
                i -= 2;
            }
            break;
        }
    }
}

Debug.WriteLine(racksUKL.Count());

And the "makrUKL" void:

void markUKL(int id)
        {
            for (int i = 0; i < racksUKL.Count; i++)
            {
                if (racksUKL[i].Id == id)
                {
                    racksUKL[i].IsSequence = true;
                    break;
                }
            }
        }

As you can see, in loop I do change in some elements in "racksUKL". But, before loop, in "racksUKL" list I have 336 elements, but after 148 elements.

Why when I change element, it's removed from list?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
krecha07
  • 31
  • 7
  • 1
    Is `racksUKL` referring to the same list as `racksUKLDraft`? – germi Dec 03 '19 at 10:27
  • No, these are two different lists, but racksUKLDraft is a copy of rackUKL. On the top of my code I have `racksUKLDraft = racksUKL` – krecha07 Dec 03 '19 at 10:29
  • 2
    then it´s **not** a copy, but another **reference to the exact same list**. – MakePeaceGreatAgain Dec 03 '19 at 10:34
  • 2
    "On the top of my code I have racksUKLDraft = racksUKL" this line of code which is *missing* in your post is the root of the problem. would be very good to include it in your post – Mong Zhu Dec 03 '19 at 10:36
  • `=` will **not** create a new list. It just references an existing one. So everything you do on one reference is reflected on the other one. – MakePeaceGreatAgain Dec 03 '19 at 10:49
  • 1
    I added in my post the definition of my lists. But, i changed my code: `racksUKLDraft = racksUKL.Select(racks => new Rack(racks)).ToList();` and it's works! before and after I have the same amount of elements. Thanks a lot! – krecha07 Dec 03 '19 at 11:40
  • splendid! the `.ToList()` call creates a new instanse of a list and since you even create new objects with the constructor: `new Rack(racks)` you really get new instances of each object! real copies. bravo – Mong Zhu Dec 03 '19 at 12:27
  • do you have any properties inside `Rack` that are not of value type like `bool` or `double` ? but of reference type like `Rack` itself? if yes then you should be careful when you simply assign those references in the copy constructor. The same hold here too, you only reference it to the same object – Mong Zhu Dec 03 '19 at 12:29
  • Hmmm.. I have `DateTime`, so what I should do it? – krecha07 Dec 03 '19 at 13:47

0 Answers0