KeyNotFoundException in manually ordered list

KeyNotFoundException in manually ordered list

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


Michael posted on Tuesday, May 25, 2010

There are a couple of other posts which discuss similar issues, but I haven't been able to solve this.

I have a SortOrderListBase class which inherits from BLB with the following methods (based on other posts):

public void MoveUp(ISortOrder item)
{
    if (item.SortOrder == 0)
        return;

    int index        = IndexOf((C)item);
    var temp         = this[index - 1];
    Items[index - 1] = this[index];
    Items[index]     = temp;

    int sortOrder = item.SortOrder - 1;

    var previous = GetItemBySortOrder(sortOrder);

    if (previous != null)
        ++previous.SortOrder;

    item.SortOrder = sortOrder;
}

public void MoveDown(ISortOrder item)
{
    if (item.SortOrder == Count - 1)
        return;

    int index        = IndexOf((C)item);
    var temp         = this[index + 1];
    Items[index + 1] = this[index];
    Items[index]     = temp;

    int sortOrder = item.SortOrder + 1;

    var next = GetItemBySortOrder(sortOrder);

    if (next != null)
        --next.SortOrder;

    item.SortOrder = sortOrder;
}

I realise there are multiple ways to approach this, but for reasons which I can't really recall, I chose to physically order the list, and it seemed others had had success this way.

The problem is if you move an item down, then remove it and save the collection, KeyNotFoundException is thrown by _map[_list[ i ]]-- in PositionMap. Moving an item up, removing it and saving does not throw. The list is wrapped in a SortedBindingList and I'm using CSLA 3.7.

Thanks in advance.

Michael

RockfordLhotka replied on Wednesday, May 26, 2010

This is the type of problem that drove me away from reordering the lists in place like I did in older versions of CSLA. It is far simpler to create a sorted view rather than to sort the list itself.

Though it probably doesn't help you, CSLA 4 does away with indexing, so this particular problem will be gone at that point.

Also, there've been numerous bug fixes around indexing in 3.8, and so it is possible that the issue would go away if you upgrade to 3.8.3.

Michael replied on Wednesday, May 26, 2010

Thanks, Rocky. We will upgrade to CSLA 4 once it's out of beta, and I'll upgrade to 3.8.3 now.

Copyright (c) Marimer LLC