473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Refactoring Collections

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?
Dec 26 '07 #1
6 1326
"Andrew Hayes" <An*********@discussions.microsoft.comwrote in message
news:75**********************************@microsof t.com...
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.

Dec 26 '07 #2
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<TaskqueuedTasks;

But I don't know how I would replace the Add method with my own.

Dec 26 '07 #3
"Andrew Hayes" <An*********@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
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;
}
}
Dec 26 '07 #4
Thank you. That answers my question. By inheriting from the typed generic
collection I can save myself a lot of drudgery.

"Alberto Poblacion" wrote:
"Andrew Hayes" <An*********@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
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;
}
}
Dec 26 '07 #5
Alberto Poblacion <ea******************************@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.

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<Taskwill be
able to bypass the check.

System.Collections.ObjectModel.Collection<Twould be a better base
class in this situation.

--
Jon Skeet - <sk***@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
Dec 26 '07 #6
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Alberto Poblacion <ea******************************@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.

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.
Not only that but they do not provide an event for that :(
Dec 26 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guenther Schmidt | last post by:
Hi, does anyone know a good PHP IDE with refactoring capabilities? Refactoring meaning things like moving methods around from a subclass into the superclass, renaming methods and local...
9
by: Peter Dembinski | last post by:
I am trying to write Master Thesis on refactoring Python code. Where should I look for information? -- http://www.dembiński.prv.pl
4
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
0
by: Andre Baresel | last post by:
Hello together, just a year ago I was searching arround for a tool supporting refactoring for c++. I've seen implementations for java and was impressed how an IDE can help with such a feature....
2
by: Sachin Garg | last post by:
Hi, I was trying to find (like many others here) a tool for refactoring C++ code as I have lately been noticing that I spend most of my coding time doing refactoring and some refactoring which...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
8
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
2
by: HendrikLeder | last post by:
Hello everybody :-) Next year I`ll write my diploma in computer science for business (It`s a degree in Germany) and I`ve some questions about the topic. The diploma will handle about refactoring...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.