Why my rows keep the previous numbers added instead of working in a new row, as I assigned it at the beginning

Hi everyone,

I did not found anything related to this on the forum, I hope I did not miss it…

I’m working on the last UiPath Studio version (2019.1.0) and I try to insert numbers in a datatable in a way that my “1” is in row(1) column(1), my “2” is in row(2) column(2) etc., until 15. I assign my new line out of the loop, thinking it would work. Instead of the result I was expecting, each row has kept the numbers previously added in the one before, like this:
, , , , , , , , , , , , , ,
1, , , , , , , , , , , , , ,
1,2, , , , , , , , , , , , ,
1,2,3, , , , , , , , , , , ,
1,2,3,4, , , , , , , , , , ,
etc. (see Array-test1.xaml (10.8 KB) and Array-test2.xaml (10.7 KB) )

I’ve changed the assign, put it inside the loop, and now I have the result I wanted to:
, , , , , , , , , , , , , ,
1, , , , , , , , , , , , , ,
,2, , , , , , , , , , , , ,
, ,3, , , , , , , , , , , ,
, , ,4, , , , , , , , , , ,
etc. (see Array-test3.xaml (10.8 KB) )

Could anyone explain me why it works only when I put the assign inside the loop? In my mind, the two first trys seem good enough. Is it a bug or I’ve missed something?

Thanks a lot,
Jb

Because when you assign myarray = newline, they are actually referencing to the same object… It will be reflected accordingly when either myarray or newline is updated with changes. refer to the image. Your case would be the one on the left. Do a read up on “reference type vs value type”