473,804 Members | 4,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
CreateCaseInsen sitiveHashtable 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 1678
You're looking in the wrong Namespace. Look in
System.Collecti ons.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******@blais esoft-xxx.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.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
CreateCaseInsen sitiveHashtable 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<stri ng, MyObjectType> myList = new Dictionary<stri ng,
MyObjectType>() ;

If you need a case insensitive key, you need to create a class that
implements the IEqualityCompar er 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<stri ng, MyObjectType> myList = new
Dictionary<stri ng,MyObjectType >(new MyEqualityCompa rerClass());

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 CaseSensitiveKe ys 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.Obj ectModel.Collec tion(Of Parameter)

With private members:
Private m_oHash As Hashtable
Private m_bCaseInsensit ive As Boolean
and use of CollectionsUtil .CreateCaseInse nsitiveHashtabl e

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.Collecti ons.CollectionB ase

Private m_oHash As Hashtable
Private m_bCaseInsensit ive 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.Collecti ons.Specialized .NameObjectColl ectionBase

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_bCaseInsensit ive 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******@blais esoft-xxx.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.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
CreateCaseInsen sitiveHashtable 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
10850
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 absolute file name of a .class file eg. C:\myJava\myApp.java, then how do i translate this file name to a java class name the Object.forName method would accept has it's parameter? thanks,
10
1881
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" vs. "!= None" ...} This seemed like a good idea at the time :(). Twice, recently, however, as my app grew, I thought, hmm... it would make things clearer if I gave
6
2319
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 get comments about coding style. I'm especially unsure about how a class should be documented, what to put in, and where. When to use double quotes, and when single. For instance, the doc string at the top must be in double quotes, or else the...
166
8695
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
1628
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 main problem is that the mem usage continues to grow until the Service stops responding. I have received advice to: "create those objects at a class level; instantiate them when the service starts, and dispose of them when the service ends. Then...
6
1741
by: Indraseena | last post by:
Hi friends, Can anybody answer me where exactly is the private inheritance is used? example: class Base { public :
2
2393
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 astronomical after a few hours. (1.5-2 Gig) After a good amount of digging I found that the memory usage is is all in System.String and contained within the pricing object. This object makes extensive use of a single string object which is...
1
2876
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 Memory Usage increases accordingly (67,730 Kb). Now when I lock and unlock the System the CPU usage increases to 50% with the Memory Usage being 67,730 kb.
2
3285
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 attributes successfully (ManagementKey, ManagementProbe, ManagementBind...). The only one attribute I am having problem with is ManagementCreate. As I understand from the documentation, it is supposed to support creation of new instances of a...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.