How do I remove an item from a list

How do I remove an item from a list

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


JohnH posted on Wednesday, February 24, 2010

Hi,

I have a collection that derives from BusinessListBase. How do I remove an item from the collection so that I can add it to another colection? When I try, it wants to mark the item as deleted and add it to the deleted list. I don't want to do this, I just want to move it to a different list.

Thanks.

ajj3085 replied on Wednesday, February 24, 2010

Csla doesn't support that concept.  At item can have one and only one parent in its lifetime.  You could create a new item with the same values (but probably not the same key values) and put that in the list, then do the delete.

See this thread for more details regarding the "one parent" statement.

RedShiftZ replied on Wednesday, February 24, 2010

Wondering out loud here, because I did something vaguely similar in my app....

Could you not do something along these lines?

//Get the Object...

MyObject obj = MyList.GetObjectByID(id);

//Make a non list copy

MyObject obj2 = obj.Clone();

//Add the object to the new list

myList2.Add(obj2);

obj.Delete();

//Flush the Delete

myList = myList.Save();

//Write the new DB records

myList2 = myList2.Save();

Like I said, since I did something similar, and it works for me... I'd like to know the downfall of this. (Besides the 2 database hits)

 

 

RockfordLhotka replied on Wednesday, February 24, 2010

You can probably do this, as long as you know what you are doing Smile

  1. Make sure the child object is not bound to a Windows Forms UI
  2. Call BeginEdit() on the child item to elevate its edit level
  3. You'll need a method on the source list that really removes an item from that list - by first removing it and then removing it from DeletedList; though this will leave the child's IsDeleted value as true
  4. Call CancelEdit() on the child item to reset its IsDeleted property to false
  5. Add the child to the new list

Assuming you are using CSLA .NET 3.8 this should work, because when you add the child to the new list, the new list should change the Parent property and edit level to match that of the new list.

The only thing I don't know for sure is whether the source list will properly unbind the child's PropertyChanged and ChildChanged events. I think that happens when the child is moved to DeletedList, but it might not happen - because it wouldn't be really necessary - so you'd have to check that.

The safer solution is to do a Clone() as you suggest, because the clone won't have any legacy event hookups to worry about, nor will its IsDeleted flag be set.

You still need to make sure to completely remove the old child from the original list - both the list and DeletedList, or you could find your being deleted out from under you unexpectedly when the original list is saved.

JohnH replied on Thursday, February 25, 2010

Thanks, that seems to work OK. I'm not actually using the PropertyChanged/ChildChanged events on this object so thats not an issue.

RockfordLhotka replied on Thursday, February 25, 2010

JohnH

Thanks, that seems to work OK. I'm not actually using the PropertyChanged/ChildChanged events on this object so thats not an issue.

It doesn't matter if you use them or not - those events get hooked by the collection, and if they don't get unhooked you could end up with some really strange results and/or memory leaks.

Copyright (c) Marimer LLC