1

To begin with I'd like to state that I have went over all other similar questions on StackOverflow such as this, this and this which may make this question appear as a duplicate.

In those issues, problem seemed to be overwriting the same instance each time and the fix to it was to instantiate new object within the loop. Other fault was incorrect static declaration. Those remedies did not work out for me.

This is a bit of background code to the fragment causing me a headache.

    // list of bytes of messages parsed from RS232 feed from a single frame
    List<byte> byte_mes = new List<byte>();

    // list of parsed messeges
    List<MessageVM> Messages = new List<MessageVM>();

After some additional logic in which byte_mes gets filled in.
This is the code which adds instances of MessageVM to the List<MessageVM> Messages.

for(loop) {

    Console.WriteLine("Received: " + BA2HexStr(byte_mes));
    Console.WriteLine("----------------------------------------");

    // new instance with current parameters
    var PenIsland= new MessageVM(byte_mes, message_id);

    // add new instance to the listt
    Messages.Add(PenIsland);

    Console.WriteLine("Messages vector size = " + Messages.Count);

    int o = 0;
    foreach(MessageVM m in Messages) {
        Console.WriteLine("Payload in Messages[{0}] {1} " ,o, BA2HexStr(m.PayLoad));                            
        o++;
    }

    // clears messages that were just uploaded
    byte_mes.Clear();                        
} 

Initially I have tried this code Messages.Add(new MessageVM(byte_mes, message_id));, but to same outcome and hence decided to go more verbose.

This is the prinout I receive from the console output after execution.
Messages are all the same besides the counter in the first byte. It loads each message successfully and it also adds the first one as intended.
On subsequent iterations the size of the List<MessageVM> Messages grows, but now each of the values it holds is replaced by the newest arrival.

Received: 30-02-28-88-88-88-88-88-88-88-88
----------------------------------------
Messages vector size = 1
Payload in Messages [0] 30-02-28-88-88-88-88-88-88-88-88 

Received: 31-02-28-88-88-88-88-88-88-88-88
----------------------------------------
Messages vector size = 2
Payload in Messages [0] 31-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [1] 31-02-28-88-88-88-88-88-88-88-88 

Received: 32-02-28-88-88-88-88-88-88-88-88
----------------------------------------
Messages vector size = 3
Payload in Messages [0] 32-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [1] 32-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [2] 32-02-28-88-88-88-88-88-88-88-88 

Received: 33-02-28-88-88-88-88-88-88-88-88
----------------------------------------
Messages vector size = 4
Payload in Messages [0] 33-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [1] 33-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [2] 33-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [3] 33-02-28-88-88-88-88-88-88-88-88 

Received: 34-02-28-88-88-88-88-88-88-88-88
----------------------------------------
Messages vector size = 5
Payload in Messages [0] 34-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [1] 34-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [2] 34-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [3] 34-02-28-88-88-88-88-88-88-88-88 
Payload in Messages [4] 34-02-28-88-88-88-88-88-88-88-88 

I believe that I do instantiate new object on each loop so not sure why previous values of the List get overwritten, while the List itself is declared outside of the loop. Any help will be much appreciated.

mega_creamery
  • 667
  • 7
  • 19
  • You create a new `MessageVM` object each time, yes, but all `m.PayLoad` refer to the same `List byte_mes` instance. `byte_mes` is being modified to the new value, so that explains the results you get. – user247702 Nov 07 '18 at 15:37
  • Ok that makes sense. Now I see they were all refeering to the same instance, and hence changed when i changed. That was a massive overlook. Thank you. – mega_creamery Nov 07 '18 at 15:42

1 Answers1

2

Try to replace

var PenIsland = new MessageVM(byte_mes, message_id);

with

var PenIsland = new MessageVM(new List<byte>(byte_mes), message_id);

This assigns each MessageVM object its own List<byte> instance.

adjan
  • 13,371
  • 2
  • 31
  • 48