type-safe collection

G

Gary

Newbie: I would like to make a strongly typed, sortable collection by
leveraging a
Framework class. I began by looking at CollectionBase but it doesn't have
built-in sorting. I would prefer to derive from ArrayList, but if I code
an Add(MyElement) method, the original Add(object) remains exposed,
compromising type safety. I know this must be a common task, how to do it?
Thanks,
Gary
 
D

David Browne

Gary said:
Newbie: I would like to make a strongly typed, sortable collection by
leveraging a
Framework class. I began by looking at CollectionBase but it doesn't have
built-in sorting. I would prefer to derive from ArrayList, but if I code
an Add(MyElement) method, the original Add(object) remains exposed,
compromising type safety. I know this must be a common task, how to do
it?

Override Add(Object) and check the type of the input object, and throw an
exception if it is the wrong type. Not a perfect solution, since it is a
run-time error, but strongly-typed collections won't be right until
generics, anyway.

David
 

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