Collections and implicit cast

B

Bratislav Jevtic

hi,

Let's suppose I've got 2 classes: MyClass and MyClassColl (coll is inherited
from ArrayList, for instance). But, when I invoke GetXXX or something
returning object, I receive an object - not instance of MyClass. I've tried
with implicit conversions - not working.

Is there any way to inherit some collection that will have instances of
custom class and make implicit conversion for return values? Practically, I
need typed collection. Because there's no such thing in C#, I'm trying to
find workaround. Can implicit conversions help me? This thing would save lot
of effort.

thanks in advance,
bj
 
N

Nicholas Paldino [.NET/C# MVP]

Bratislav,

Implicit conversions would not help you. The easiest (but tedious) way
of doing this is deriving from the CollectionBase class. You will have to
create type-safe versions of accessor methods and the like. However, the
implementations of the enumerator, and interface implementations are
handled.

However, with Generics, this will become very, very easy, but we will
have to wait for that.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top