Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2007

Tip: Looking for answers? Try searching our database.

Refactoring Collections

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew Hayes - 26 Dec 2007 08:07 GMT
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?
Alberto Poblacion - 26 Dec 2007 08:42 GMT
> Maybe I'm missing a code snippet or something, but there doesn't seem to
> be a
[quoted text clipped - 14 lines]
> 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.
Andrew Hayes - 26 Dec 2007 09:08 GMT
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.
Alberto Poblacion - 26 Dec 2007 09:56 GMT
> 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 :(

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.