Creating Custom Collection

G

Guest

Dear All,

I am trying to decide on to create a collection object for a project that I
am working on. I am fairly new to OOP so this may be on the basic side. I
have looked on the groups, but can't seem to find the answer I am looking for
- just more questions!

I want an object to hold Case details, one of the properties of this Case
class will be RequestedBy. I ideally want this property to be a collection
of User Objects (which will contain name, telephone, department etc).

My thinking is that I would have a Class Case with a property of RequestedBy
(of type Users) and the Users collection will have an indeterminate number of
User objects.

What is the best way for me to create the Users collection? From what I
have read I can either:
Create my own Collection by implementing the IEnumerable/IEnumerator interface
OR
Implement a pre-written Collection Interface for example IDictionary
OR
Implement the Class, for example Dictionary and my calls to base.Add for
example.

Is there a preferred way to do this? It seems to be such a fundamental
thing to do and whilst there is a great deal of information on the different
methods, there doesn't seem to be anything on how and when to use different
strategies.

I look forward to any help!

Regards

Mike Swann
 
C

Claes Bergefall

It depends on what version of the framework you're using

2.0
Use System.Collections.Generics.List<User> directly (i.e. no need to define
a new collection class).

1.1
Inherit a new class from CollectionBase (do a search in the docs and you
should find an article on how to do that)


/claes
 
L

Linda Liu [MSFT]

Hi Mike,

Thank you for posting.

You needn't create your custom user collection. .NET has provided several
kinds of collections for you, which you can use directly.

If you are using VS.NET2003, you may choose ArrayList or Hashtable(in
System.Collections namespace).
If you are using VS2005, you may choose List<T> or Dictionary<T>(in
System.Collections.Generic namespace).

FYI, ArrayList and List<T> are used when you have an ordered collection of
items that do not have a key. Hashtable and Dictionary<T> are used when you
have an unordered collection of items that have a key.

Hope this helps.
If you have anything unclear, please don't hesitate to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi Linda,

Thanks for your help.

I am thinking about using the Dictionary Class, however should I be
inherting the class and exposing the base methods (ie base.Add) or should I
be creating a local dictionary variable and then exposing the local methods
(ie localDictionary.Add).

It's more the theory of OO that I am struggling with I think!

Any help would be greatly aprreciated!

Thanks

Mike
 
L

Linda Liu [MSFT]

Hi Mike,

Thanks for your quick response.

I don't think you should inherite the Dictionary<T> class. You could use it
directly in your program. Just define a local dictionary variable of type
Dictionary<T> in your class and write a property to expose it. The code
may be something like below:

public class Case
{
private Dictionary<string,User> users;
public Case()
{
users = new Dictionary<string,User>();
}
public Dictionary<string,User> Users
{
get { return users; }
}
}

You can access the Users property outside the class Case(but can't set the
property in this case for there's no set procedure in this property) and
call all the methods of Dictionary class.

Hope this helps.
If you have anything unclear, please don't hesitate to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
J

Joanna Carter [TeamB]

"MikeSwann" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I am thinking about using the Dictionary Class, however should I be
| inherting the class and exposing the base methods (ie base.Add) or should
I
| be creating a local dictionary variable and then exposing the local
methods
| (ie localDictionary.Add).

If you are wanting a "lookup" based on a key, then you certainly need a
Dictionary. The question remains, which version of .NET are you working
with, as this decides whether you use a generic collection or not.

| It's more the theory of OO that I am struggling with I think!

You would never normally inherit from a list class in order to create a
container class. Instead you would use either Aggregation or Composition.

The choice is down to whether the containing class "owns" the items in the
list.

Composition means the contained items are owned by the containing class.
They have no context outside of the containing class; they are created,
managed and destroyed only from within the containing class.

Aggregation means that the items in the list have their own lives outside of
the containing class. The list is essntially a list of references to
instances of other classes.

Joanna
 

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