473,405 Members | 2,373 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,405 software developers and data experts.

CollectionsUtil class ... usage?

I want to create a collection class that will be strongly typed (store a
specific object type), be keyed with a case insensitive string, and be able
to access objects stored by index, or sequentially (in the order stored) via
"For Each".

I know I could code this from scratch - or derived from a number of
framework classes, but I'm not sure of the pros/cons of various
possibilities.

I'd like to use some of the new Generics - they are sweet, but there is no
generic collection - they have dictionaries, lists, sorted list, etc.

Perhaps I can add a HashTable to a basic (derived) collection class.

But now I see this CollectionsUtil class, which has a
CreateCaseInsensitiveHashtable method. The docs on it are a bit sparse, no
usage examples. Can this be used with existing collection or dictionary
classes? If I had to, I could give up the indexed access requirement - and I
guess all of the collection classes provide enumerators so For Each and
foreach can be used (although with dictionaries, you are iterating over
DictionaryEntry items, not the stored objects).

I'd appreciate any advice/comments.

Thanks
May 5 '06 #1
4 1663
You're looking in the wrong Namespace. Look in
System.Collections.ObjectModel. The Collection<T> class sounds like just
what you need.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Mark" <ma******@blaisesoft-xxx.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
I want to create a collection class that will be strongly typed (store a
specific object type), be keyed with a case insensitive string, and be able
to access objects stored by index, or sequentially (in the order stored)
via "For Each".

I know I could code this from scratch - or derived from a number of
framework classes, but I'm not sure of the pros/cons of various
possibilities.

I'd like to use some of the new Generics - they are sweet, but there is no
generic collection - they have dictionaries, lists, sorted list, etc.

Perhaps I can add a HashTable to a basic (derived) collection class.

But now I see this CollectionsUtil class, which has a
CreateCaseInsensitiveHashtable method. The docs on it are a bit sparse, no
usage examples. Can this be used with existing collection or dictionary
classes? If I had to, I could give up the indexed access requirement - and
I guess all of the collection classes provide enumerators so For Each and
foreach can be used (although with dictionaries, you are iterating over
DictionaryEntry items, not the stored objects).

I'd appreciate any advice/comments.

Thanks

May 5 '06 #2
V
Hi Mark,

As I understand it:
1. Typed Collection - Generics allows you to do this.
2. Keyed Collection - It has to be some kind of dictionary, i think the
Hashtable is a good example.
3. Case Insensitive - A custom implementation with an internal use of
the hashtable (to handle the case-insensitive key part) or even a
derived one.
4. Enumerable by index - Maybe use a SortedList flavor instead of a
hashtable (slower though). - Or maybe again customize the hashtable to
suit your needs.

I mean, why implement something of your own, when you can twist and
bend what is provided.

Regards,
Vaibhav
www.nagarro.com

May 5 '06 #3
Use the Generic Dictionary class:

Dictionary<string, MyObjectType> myList = new Dictionary<string,
MyObjectType>();

If you need a case insensitive key, you need to create a class that
implements the IEqualityComparer interface. This class will have an
Equals method that returns true if two keys are equal.

Then you can create a generic Dictionary to use it:

Dictionary<string, MyObjectType> myList = new
Dictionary<string,MyObjectType>(new MyEqualityComparerClass());

May 5 '06 #4
Hey everyone,

Thanks for your thoughts. I did some experimenting and found that Kevin was
pretty much on the mark.

I tried a bunch of approaches just to see what would work well. My
requirements were that the class have the following features:
- a strongly typed (homogenous) collection - my test used a class named
Parameter
- an arbitrary string key that is associated with each object added to the
collection
- the key must be case insensitive (wound up making this optional)
- must be able to iterate of objects in collection
- must be able to access objects by index or key
- the order of objects in the collection must match the order in which they
were added (excepting insertions)

Methods/properties (all params are ByVal)
New 'parameterless constructor, yields case insensitive keys
New(ByVal CaseSensitiveKeys As Boolean) 'caller specifies case
sensitivity of keys
Add(Key As String, Item As Parameter)
Clear
Contains(Key As String)
Contains(Item As Parameter)
Count
IndexOf(Key As String)
IndexOf(Item As Parameter)
Insert(Key As String, Item As Parameter, Index As Integer)
Default ReadOnly Property Item(Key As String)
Default ReadOnly Property Item(Index As Integer)
Remove(Key As String)
Remove(Item As Parameter)
RemoveAt(Index As Integer)
1) Kevin's suggestion is probably the most workable, and cool because it
uses generics :-) I used the Shadows keyword with the Add, Clear, Insert,
Remove and RemoveAt methods so I could prevent usage of these methods in the
base class, and allow my derived class to accept Key args and manage the
hash table.
Inherits Collections.ObjectModel.Collection(Of Parameter)

With private members:
Private m_oHash As Hashtable
Private m_bCaseInsensitive As Boolean
and use of CollectionsUtil.CreateCaseInsensitiveHashtable

This approach involved the least amount of code (about 85 lines)

2) a close second is the "standard" approach of implementing a strongly
typed collection, available since v1 of the framework:
Inherits System.Collections.CollectionBase

Private m_oHash As Hashtable
Private m_bCaseInsensitive As Boolean = True

This is very similar to the first option, except that one has to implement
the OnInsert, OnSet and OnValidate overrides, to insure that only objects of
the proper type are added to the collection. and we had to cast returns to
the proper type. Here, we only had to use Shadows for Clear and RemoveAt.

3) This looked great on paper:
Inherits System.Collections.Specialized.NameObjectCollectio nBase

This class was meant to do almost exactly what I wanted, except only with
case sensitive keys. I worked around that limitation by lowering the case of
all passed Key parameters where case insensitive keys were selected.

This worked well, and was actually the shortest and cleanest implementation,
except that class' Enumerator would only allow iteration over the keys, not
the objects, so I discarded this after considering, but deciding not to,
rectify this limitation with a custom enumerator (I was running out of
experimentation time - have to get some work done at some point ;-)

4) Another option was simply a custom wrapper class around a HashTable
object
Implements IEnumerable

Private m_oHash As Hashtable
Private m_bCaseInsensitive As Boolean = True

This actually worked pretty well, and it obviously required no use of
Shadows, but it suffered the same fate as #3, a hash table is essentially a
dictionary, and its enumerator iterates over dictionary entries, not the
objects.

Thanks again for the feedback.

Cheers ...

"Mark" <ma******@blaisesoft-xxx.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
I want to create a collection class that will be strongly typed (store a
specific object type), be keyed with a case insensitive string, and be able
to access objects stored by index, or sequentially (in the order stored)
via "For Each".

I know I could code this from scratch - or derived from a number of
framework classes, but I'm not sure of the pros/cons of various
possibilities.

I'd like to use some of the new Generics - they are sweet, but there is no
generic collection - they have dictionaries, lists, sorted list, etc.

Perhaps I can add a HashTable to a basic (derived) collection class.

But now I see this CollectionsUtil class, which has a
CreateCaseInsensitiveHashtable method. The docs on it are a bit sparse, no
usage examples. Can this be used with existing collection or dictionary
classes? If I had to, I could give up the indexed access requirement - and
I guess all of the collection classes provide enumerators so For Each and
foreach can be used (although with dictionaries, you are iterating over
DictionaryEntry items, not the stored objects).

I'd appreciate any advice/comments.

Thanks

May 8 '06 #5

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

Similar topics

8
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
10
by: george young | last post by:
I had developed the habit of using the neat python form: if someinstance: someinstance.memb() because it seems cleaner than "if someinstance is not None". {please no flames about "is not None"...
6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
2
by: Fish | last post by:
I have been researching the correct way to organize my solution so that it makes best use of VB.NET inherent ability to manage resources such as objects. My solution contains 2 projects and the...
6
by: Indraseena | last post by:
Hi friends, Can anybody answer me where exactly is the private inheritance is used? example: class Base { public :
2
by: miguel | last post by:
Hi, I have created a service which parses real-time prices from one format into another. This is all working very quickly and nicely, however, I have recently found that the memory usage is...
1
by: sowmya.rangineni | last post by:
Ours is a windows based application. When we open the application the CPU usage is 0% and the Memory Usage is 54,324Kb When I open a specific form in a module, the CPU usage is 0% and the...
2
by: =?Utf-8?B?dXJrZWM=?= | last post by:
I am trying to create an in-process WMI provider using System.Management.Instrumentation namespace. For testing I use a simple class as a wrapper for FileInfo class. I have been able to use all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.