Abstract Base Collection

A

aa7im

I am attempting to create a base collection that I can use to derive
strongly typed collection. My base collection class will derive from
CollectionBase and I want my typed collections to be forced to
implement certain methods:


public abstract class MyCollectionBase: CollectionBase
{
public void Sort() {.... SORT IMPLEMENTATION }
public void Filter() {...> FILTER IMPLEMENTATION }
}


Sort and Filter can be done generically so I am including those in my
base class design. Now I want to force my derived classes to implement
the strongly typed Add(typedObject object), Remove(), this[int index]
members....

I am not sure how to make the derived classes be forced to implement
the interface...

public abstract int Add(...... DON'T KNOW WHAT TO PUT HERE .....)
{
return List.Add(...);
}

When I define the methods it makes me define the types for the
parameters and then I can't implement a typed Add(), this[] methods in
my derived classes.... Interfaces seem to have the same issue because
I have to define the "types" of the methods, etc..
I hope this all makes sense to somebody...

Thanks,
Josh
 
P

Peter Rilling

You probably cannot force a method to be implemented unless you give it the
exact signature, which means that you know what types will be passed. That
might be one reason why CollectionBase does not force the methods to be
implemented (they are implemented as implicit methods). The problem is
that, if I understand you correctly, you do not know what type will be added
to the collection until someone derives a class.

Now, you could always use "object" in the signature, which of course defeats
the purpose of trying to enforce a strongly typed collection.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Unforturnally what you want is not possible with the actual implementation
(it will be doable with C# 2.0 ).

As you don;t know the exact signature of the method you cannot declare it a
priori.


Cheers,
 
A

aa7im

I was hoping that was not the case.... I guess I will hope the
developers will remember to implement all collections the same...
That sucks...
 
D

Dennis Myrén

My approach for strongly typed collections is i have a template class,
where i use <T> everywhere to reference the type.
To implement the collection for a type, i copy the template
and replace all occurrences of <T> with the type name.
Done in 30 seconds.
This is really the best solution, since i would not think of using
System.Collections.CollectionBase
and since Generics is not yet present.
 
A

aa7im

I am currently using CodeSmith to produce my collections using a
template that I wrote. Works great, but seemed like their was a more
OOP way of forcing implementation details for the derived classes....
 

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