R rodmcm Active Member Licensed User Longtime User Nov 16, 2018 #1 This is cut down program to demonstrate a fault I just cant fathom The program simply deletes a record (No 17 for an example) in an array of 20 item by shuffling the other array values down and then assigning a default value to the last record. So values for 18 goes to 17, 19 goes to 18, 20 goes to 19 All well and good until I put the last record(20) values to the default 9999.. Both the 19 and 20th record values go to 9999. Same happens if I put the 19 record to 9999..... What is going on please? The screen shot is of the logs.. This happens on B4A and B4J Attachments Capture.JPG 38.4 KB · Views: 483 Delete_Array_Line.zip Delete_Array_Line.zip 2.2 KB · Views: 373
This is cut down program to demonstrate a fault I just cant fathom The program simply deletes a record (No 17 for an example) in an array of 20 item by shuffling the other array values down and then assigning a default value to the last record. So values for 18 goes to 17, 19 goes to 18, 20 goes to 19 All well and good until I put the last record(20) values to the default 9999.. Both the 19 and 20th record values go to 9999. Same happens if I put the 19 record to 9999..... What is going on please? The screen shot is of the logs.. This happens on B4A and B4J
stevel05 Expert Licensed User Longtime User Nov 16, 2018 #2 It's because you are trying to copy objects, which only copies a pointer to the object, so two array elements end up pointing to the same array. Your Shuffle code should be : Code: Copy ' shuffle the records down, only to the second to last record For j = TypeSelection To Profile.Length-2 Profile(j).ItemNo = Profile(j+1).ItemNo Profile(j).Cost = Profile(j+1).Cost Next Upvote 0
It's because you are trying to copy objects, which only copies a pointer to the object, so two array elements end up pointing to the same array. Your Shuffle code should be : Code: Copy ' shuffle the records down, only to the second to last record For j = TypeSelection To Profile.Length-2 Profile(j).ItemNo = Profile(j+1).ItemNo Profile(j).Cost = Profile(j+1).Cost Next
R rodmcm Active Member Licensed User Longtime User Nov 19, 2018 #3 Sorry for delay in replying..Thanks for that it worked but I do not understand your comment about two element arrays pointing the the same array. In my tiny brain each element of the array references a profile(j) that has the object data associated with that array item.. Can you explain further please Upvote 0
Sorry for delay in replying..Thanks for that it worked but I do not understand your comment about two element arrays pointing the the same array. In my tiny brain each element of the array references a profile(j) that has the object data associated with that array item.. Can you explain further please
stevel05 Expert Licensed User Longtime User Nov 19, 2018 #4 stevel05 said: two array elements end up pointing to the same array Click to expand... Should have been 'pointing to the same object.' rodmcm said: each element of the array references a profile Click to expand... Yes the key word is references, not contains. After applying Profile(j) = Profile(j+1) As the objects are not moved, Element A is no longer referenced and Elements 3 and 4 are both referencing Object E Upvote 0
stevel05 said: two array elements end up pointing to the same array Click to expand... Should have been 'pointing to the same object.' rodmcm said: each element of the array references a profile Click to expand... Yes the key word is references, not contains. After applying Profile(j) = Profile(j+1) As the objects are not moved, Element A is no longer referenced and Elements 3 and 4 are both referencing Object E