Refactoring Collections

A

Andrew Hayes

Maybe I'm missing a code snippet or something, but there doesn't seem to be a
way in VS2005 to create a class that is a collection of another class.
Something that was very easy to do in the old VB6 Class Builder.

I've tried using the Class Designer but while it handles many of the
refactoring needed for properties and methods, it doesn't have the templates
needed for large class code creation.

At the moment I'm manually coding the collection wrapper for each of the
classes I'm working with, although I have shortened my time by copying and
pasting a completed collection class and just doing a search and replace to
change the name.

Should I be using a class inherted from CollectionBase to store the
collection of my other class, or should I just use the base classes?
 
A

Alberto Poblacion

Andrew Hayes said:
Maybe I'm missing a code snippet or something, but there doesn't seem to
be a
way in VS2005 to create a class that is a collection of another class.
Something that was very easy to do in the old VB6 Class Builder.

I've tried using the Class Designer but while it handles many of the
refactoring needed for properties and methods, it doesn't have the
templates
needed for large class code creation.

At the moment I'm manually coding the collection wrapper for each of the
classes I'm working with, although I have shortened my time by copying and
pasting a completed collection class and just doing a search and replace
to
change the name.

Should I be using a class inherted from CollectionBase to store the
collection of my other class, or should I just use the base classes?

With the inclusion of Generics en C# version 2.0, the need for the
creation of specialized collections has almost dissappeared. If you want a
collection of elements of one of your classes, you just use one of the
collection types in System.Collections.Generic. For instance, if your class
is MyClass, you can declare a variable of type
System.Collections.Generic.List<MyClass>, and that's it: you now have typed
methods to Add, Remove, fetch, etc. items of type MyClass.
 
A

Andrew Hayes

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.
 
A

Alberto Poblacion

Andrew Hayes said:
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?

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;
}
}
 
A

Andrew Hayes

Thank you. That answers my question. By inheriting from the typed generic
collection I can save myself a lot of drudgery.
 
J

Jon Skeet [C# MVP]

Alberto Poblacion said:
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);
}

Unfortunately that won't work as Add isn't virtual.
//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;
}

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.
 

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