PC Review


Reply
Thread Tools Rate Thread

Creating Custom Collection

 
 
=?Utf-8?B?TWlrZVN3YW5u?=
Guest
Posts: n/a
 
      26th Jun 2006
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmVmZnJleSBIb3JuYnk=?=
Guest
Posts: n/a
 
      26th Jun 2006
Why not use List<UserObject, userIndex>?
--
Jeffrey Hornby
Hornby Consulting, Inc.



"MikeSwann" wrote:

> 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

 
Reply With Quote
 
Claes Bergefall
Guest
Posts: n/a
 
      26th Jun 2006
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


"MikeSwann" <(E-Mail Removed)> wrote in message
news:096DD5EF-9DCC-4ADA-ACFE-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      27th Jun 2006
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.
====================================================

 
Reply With Quote
 
=?Utf-8?B?TWlrZVN3YW5u?=
Guest
Posts: n/a
 
      27th Jun 2006

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

"Linda Liu [MSFT]" wrote:

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

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      27th Jun 2006
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.
====================================================

 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      27th Jun 2006
"MikeSwann" <(E-Mail Removed)> a écrit dans le message de news:
4A68D408-2BC6-4165-B063-(E-Mail 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

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help creating a custom collection DotNetNewbie Microsoft C# .NET 2 21st Jan 2008 09:25 PM
Creating IndexOf in a custom collection =?Utf-8?B?RGFsZQ==?= Microsoft C# .NET 7 3rd Nov 2006 06:24 AM
Creating A Custom Collection =?Utf-8?B?S2FwaWw=?= Microsoft Dot NET Framework 2 21st Sep 2004 01:09 AM
Creating custom collection type with extended mapi? Julia Microsoft Outlook Program Addins 1 1st Mar 2004 06:15 PM
Need explanation on creating a custom collection. Ken Varn Microsoft C# .NET 2 22nd Jul 2003 04:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:35 PM.