473,890 Members | 1,770 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 1682
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
10856
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
1886
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
2325
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
8726
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
1633
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
1743
by: Indraseena | last post by:
Hi friends, Can anybody answer me where exactly is the private inheritance is used? example: class Base { public :
2
2396
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
2879
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
3289
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
9815
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
10801
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...
0
10449
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
9616
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...
1
8007
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7159
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5834
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4662
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
4258
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.