DynamicListBase problem (maybe a bug)

DynamicListBase problem (maybe a bug)

Old forum URL: forums.lhotka.net/forums/t/10402.aspx

Ziad posted on Tuesday, May 31, 2011


Due to some tests that I have done, I have noticed that there may be a problem or a kind of bug in the DynamicListBase class. In the DynamicListBase.cs we have the following virtual procedure:

public virtual void SaveItem(int index)



dp.UpdateCompleted += (o, e) =>

            if (item.IsDeleted)
              //SafeRemoveItem  will raise INotifyCollectionChanged event



For example, let us suppose that we have a simple DynamicListBase which is composed of the following two elements:

          1.  Bugs Bunny

          2.  Scooby Doo

Note: The index of the Bugs Bunny element is 0 and that of Scooby Doo is 1. 

Imagine that the user, through the user interface, deletes the first element and then immediately deletes the second one. This is what happens:

1. DataPortal_Deleteitself is called on the first element "BugsBunny".

2. After that, the above virtual procedure "SaveItem" is called with an index that is equal to 0, and this is logic because the index of the first element is 0. Since the element is marked for deletion, then "item.IsDeleted" is equal to true, and as a result SafeRemoveItem(index) is called with an index = 0.

3. The first element is deleted without any errors.

4. Now its the turn of the second element. As usual, DataPortal_DeleteItself will be called on the second item.

5. SaveItem is called with an index that is equal to 1, because logically the index of the second element is 1. The second element is marked for deletion, this means that item.IsDeleted is equal to true, therefore SafeRemoveItem(index) is called with an index = 1.

6. SafeRemoveItem is executed with errors because the first element "BugsBunny" was deleted and this means that the index of the second element "Scooby doo" became 0 and not 1, and when SaveItem is called on the second element, it is called with an index=1 and not 0, and this causes a problem while removing the element in SafeRemoveItem because the list contains one element which is Scooby Doo and so index=1 does not exist in the list after deleting the first element.



RockfordLhotka replied on Tuesday, May 31, 2011

I see what you are talking about. This does appear to be a bug.

I'll add it to the bug list.

It may not be easy to fix, because we can't change the way data binding works. The "fix" might be to throw an exception if an attempt is made to perform any overlapping SaveItem calls.

Ziad replied on Tuesday, May 31, 2011

Thank you for your reply!

At these moments, I'm using the following temporary solution:

       In the SaveItem method, I have added index = this.IndexOf(item) in dp.UpdateCompleted += (o, e) =>

Thanks again


Copyright (c) Marimer LLC