On Wed, 08 Oct 2008 00:57:57 -0700, phancey <de**@2bytes.co.ukwrote:
Yes I think you are right. The only trouble is that the way everything
has already been coded in our system makes this very difficult. I
thought I could code as List<IElementbut then kept getting errors
saying that a List<Elementcould not be converted to a List<IElement>
because the methods in our existing system return List<concreteclass>.
What I need to do is adapt somehow so that I add concreteclass
instances to the List<IElementinstead of adding them to a
List<Elementand then trying to cast that back as it doesn't work
that way. Trouble is this may be very difficult with the code I have
inherited.
Yes, you're right. With a List<IElementeverywhere, you can add Element
instances as well as IElements. But you can't just go assigning a
List<Elementto a variable of List<IElementor vice a versa. If you
could, then that would open the door for code that compiles just fine but
which tries to add list items of the wrong type to the list.
Note that List<Timplements IList, and that interface supports all the
basic list operations. That's why Marc suggested using it as the type for
the variable.
Without the bigger picture, it's hard to know exactly what the right
advice is. But it seems like if you can't go back and fix all your code
so that it's using a more basic data type, or just use IList. Of course,
it's also possible that you should just skip the idea of having a
general-purpose type for the purpose mentioned in your original post.
Pete