0

I am trying to build a function that adds a class of items into another class. Anytime I add a new item, it duplicates the item however many times I have tried to add new items previously.

  myfunction()
   {
   AmazonEnvelopeMessage message = new AmazonEnvelopeMessage();
        List<AmazonEnvelopeMessage> list = new List<AmazonEnvelopeMessage>();
        AmazonEnvelopeLibrary.Models.Product product = new Product() 
   foreach (var s in skus)
        { 
   product.DescriptionData.Title = s.Title;
                product.StandardProductID.Value = s.ASIN;
                product.StandardProductID.Type = StandardProductIDType.ASIN;
                product.ProductData.Item = maincat;
                product.DescriptionData.MSRP.Value = s.MSRP;
                product.DescriptionData.Description = s.description;
           message.Item = product;
            list.Add(message);
      }
      amazon.Envelope.message.AddRange(list);
    }
liz
  • 13
  • 3
  • Don't you need to specify the key, or id of the message or item? – Afzaal Ahmad Zeeshan May 27 '17 at 08:19
  • Your code does not compile and has errors. – Svek May 27 '17 at 08:23
  • You are adding the same product object several times in the loop. You are modifying the fields of that single object, but only the changes in the last round of the loop will be effective. Also, what will happen later when a list of entries that all point to the same object is used in AddRange? – Bent Tranberg May 27 '17 at 08:25
  • I took out some parts of the code that are irrelevant to the question partially because this is for a project I'm working on for my company and I would like to keep it confidential, but give people the general idea of what is going on. basically all of the information will be serialized. – liz May 27 '17 at 08:29

2 Answers2

0

Your code has some issues, but generally here is your problem

AmazonEnvelopeMessage message = new AmazonEnvelopeMessage();

foreach (...)
{
    list.Add(message); // you keep passing reference to the same object.
}

You are passing the reference to the same object.

In order to fix it, you need to do this

foreach (...)
{
    AmazonEnvelopeMessage message = new AmazonEnvelopeMessage();
    list.Add(message); // now you're passing a new one each time
}
Svek
  • 12,350
  • 6
  • 38
  • 69
  • I tried that and its still duplicated message in list. – liz May 27 '17 at 08:34
  • Not only the _message_ variable but also the _product_ one suffers of the same problem – Steve May 27 '17 at 08:38
  • @liz this is just one class, (as noted by Steve) you have a others to consider. Make sure each one is getting new'ed up in each iteration. – Svek May 27 '17 at 08:42
0

If the 'AmazonEnvelopeMessage' and 'AmazonEnvelopeLibrary.Models.Product' are classes, then you are dealing with a reference type problem. In this case, moving the 'message' and 'product' in the foreach loop solves the problem. But it doesn't solves the actual problem. There are two ways to keep information in memory in C# ; Stack and Heap. I suggest you to take a look at this question asked by another user. It will make you understand why it happens.

myfunction()
   {
       List<AmazonEnvelopeMessage> list = new List<AmazonEnvelopeMessage>();
       foreach (var s in skus)
       { 
           AmazonEnvelopeMessage message = new AmazonEnvelopeMessage();
           AmazonEnvelopeLibrary.Models.Product product = new Product();
           ...
           message.Item = product;
           list.Add(message);
      }
      amazon.Envelope.message.AddRange(list);
    }