Thanks for the reply Alberto.
I had looked at typed collections (there is a code snippet for that, at
least), but I still need to overload the Add in a majority of cases so that I
can do additional processing before adding the class to the collection (or
not adding it, if the validation checking fails).
In this case I wouldn't be able to use a Generic collection, right?
At the moment, for example, I have a Task class that exposes various
properties, methods and functions for handling numerous tasks.
The Tasks class looks something like this:
public class Tasks : CollectionBase
{
...
public boolean Add ( Task pTaskToAdd )
{
// Do task specific checks and processing, return false if error
List.Add ( pTaskToAdd );
return true;
}
}
And used in the TaskHandler class like this:
static Tasks queuedTasks;
Task printTask = new Task(printJobID, printPrinter);
if ( queuedTasks.Add(printTask) )
// Task queued successfully
else
// Task failed to be added to task queue
If I use the generic collections, it would be something like this -
static List<Task> queuedTasks;
But I don't know how I would replace the Add method with my own.
> I had looked at typed collections (there is a code snippet for that, at
> least), but I still need to overload the Add in a majority of cases so
[quoted text clipped - 3 lines]
>
> In this case I wouldn't be able to use a Generic collection, right?
Yes, you can use the Generic collection. You can inherit from it just
like any other class, and then you can replace the Add method.
public class Tasks : List<Task>
{
//Option 1: override "Add"
public override void Add(Task p)
{
//Do specific processing
base.Add(p);
}
//Option 2: overload "Add"
public new bool Add(Task p)
{
//Similar to "override Add" but here we return a bool.
//call this.Add or base.Add to add p to the List.
return true;
}
}
Andrew Hayes - 26 Dec 2007 10:12 GMT
Thank you. That answers my question. By inheriting from the typed generic
collection I can save myself a lot of drudgery.
> > I had looked at typed collections (there is a code snippet for that, at
> > least), but I still need to overload the Add in a majority of cases so
[quoted text clipped - 23 lines]
> }
> }
Jon Skeet [C# MVP] - 26 Dec 2007 11:07 GMT
Alberto Poblacion <earthling-quitaestoparacontestar@poblacion.org>
wrote:
> Yes, you can use the Generic collection. You can inherit from it just
> like any other class, and then you can replace the Add method.
[quoted text clipped - 7 lines]
> base.Add(p);
> }
Unfortunately that won't work as Add isn't virtual.
> //Option 2: overload "Add"
> public new bool Add(Task p)
[quoted text clipped - 3 lines]
> return true;
> }
That will compile, but anyone using it as a plain List<Task> will be
able to bypass the check.
System.Collections.ObjectModel.Collection<T> would be a better base
class in this situation.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Ignacio Machin ( .NET/ C# MVP ) - 26 Dec 2007 14:17 GMT
Hi,

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> Alberto Poblacion <earthling-quitaestoparacontestar@poblacion.org>
> wrote:
[quoted text clipped - 12 lines]
>
> Unfortunately that won't work as Add isn't virtual.
Not only that but they do not provide an event for that :(