Read-only child list question

Read-only child list question

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


albruan posted on Wednesday, September 06, 2006

I have a need for read-only objects to be used for navigating through the application I'm developing.  I have a read-only root list (Projects) of read-only child objects (Project) and each of the Project objects has a read-only child list (Departments) of read-only child objects (Department).  It works ok up to a point, namely, while attempting to add a Department object to the Departments collection.  As soon as that's attempted, an exception is thrown stating that "Insert is an invalid operation".  Since this exception is thrown at this point, I'm wondering if another exception of the same type will be thrown when/if I get beyond this point and attempt to add a Project object to the Projects collection.  Anyway, I've attached copies of all four classes in the hope someone can point out what I'm doing wrong.

Many thanks!

Allen

tetranz replied on Wednesday, September 06, 2006

albruan:
As soon as that's attempted, an exception is thrown stating that "Insert is an invalid operation".

That's because a ReadOnly list is just that, its "read only" which means that you can't insert or delete items. However, its only enforced via the IsReadOnly property which has the protected modifier for set therefore you can change it inside your class. ie,  do IsReadOnly = false before the insert. I didn't download your files but if you're wanting to do that from outside the class then you're either using the ReadOnly concept wrongly or its not what you want.

Ross

albruan replied on Wednesday, September 06, 2006

Thanks!  I figured I must've been overlooking something...sure enough, I neglected to set IsReadOnly equal to false before attempting to add the items.

jkellywilkerson replied on Wednesday, September 06, 2006

You may have done this as well, but you want to make sure that you set IsReadOnly back to true right after the insert as well.

Kelly.

Copyright (c) Marimer LLC