tags:

views:

386

answers:

3

What does this mean? I am returning a IList<T> from my business layer and then adding items from the UI, but the app is complaining that it's a fixed-size list. How can I overcome this problem?

A: 

As far as I know you can't tell programmatically whether or not you'll be able to add to a writable list. On the other hand, the most common example is probably Array - so you could always try:

if (list is Array)
{
    // Copy to another list or whatever
}

Low-tech as it seems, I'd just indicate in the business layer the properties of the list you'll be returning - whether it'll be writable or not, etc. Or just assume that it won't be writable, and create a new list in the UI anyway.

Jon Skeet
More generally, you can see if the list is an instance of the non-generic System.Collections.IList interface and if so, check the IsFixedSize property.
kvb
A: 

If you have control over your business logic (e.g. it's not code-gen'd) then the easiest solution will likely be to make sure that you're using an IList<T> implementation that's not fixed size (such as a List<T>).

kvb
+1  A: 

Could you just create a new list? IE:

List<myObject> foo = new List<myObject>(someClass.getReadOnlyList(...))

If you've got to get the list back to the business logic, check to make sure there's not some other add() functionality (insert, add, append, prepend, etc). Some classes don't allow you to directly modify their internal collections as they prefer to do some sanity checking first, or perform some other type of working with the new data that might not should be exposed to the consumer.

MighMoS