Collections

  • Thread starter Thread starter Prateek
  • Start date Start date
P

Prateek

Hi,

I would like to have a collection of objects.

I need to use both Key and Index to access the collections members

At the same time I dont want them to be sorted automatically ( like in
SortedList )

Is there any built in collection type that supports this.

In VB 6 Collections were doing this. Now I dont want to use Collections as
they cannot be serialized.

Please help !

TIA,

Gary
 
HashTables are made to order on this ones.
Hi,

I would like to have a collection of objects.

I need to use both Key and Index to access the collections members

At the same time I dont want them to be sorted automatically ( like in
SortedList )

Is there any built in collection type that supports this.

In VB 6 Collections were doing this. Now I dont want to use Collections as
they cannot be serialized.

Please help !

TIA,

Gary
 
Why doesnt SortedList solve your problem? You can use the Item property to
fetch values by key and use GetByIndex to get by values by index. The
elements are automatically sorted.
SortedList is serializable and implements ICollection. So, what else is
wrong here???
 
Hi,

I would like to have a collection of objects.

I need to use both Key and Index to access the collections members

At the same time I dont want them to be sorted automatically ( like in
SortedList )

Is there any built in collection type that supports this.

In VB 6 Collections were doing this. Now I dont want to use
Collections as they cannot be serialized.

Please help !

TIA,

Gary

Yes, I have your answer. I created a hybrid collection that can be
accessed by indedx or key. It is derived from CollectionBase, so
contains an internal arraylist that is accessed by index. I also added
an internal hashtable for storing by key. This collection can be sorted
by calling sort on the internal arrayList. But, as you requested, it
does not sort automatically.

I created a generator that will create a customized version of this
collection. See 'simple code generator' in my signature. Download both
the exe, and one or more of the available templates on the download page.
The source is also available, of coarse.

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
 
Why doesnt SortedList solve your problem? You can use the Item
property to fetch values by key and use GetByIndex to get by values by
index. The elements are automatically sorted.
SortedList is serializable and implements ICollection. So, what else
is wrong here???

Maybe you missed the part where he says he doesn't want it sorted?!
 
Thanks all for the reply.
But can someone let me know how do you access an item in the hashtable using
index.

If someone can point me to a code snippet showing how you access using both
key as well as Index, it would be very helpful.

Thanks again,

Gary
 
http://www.codeproject.com/csharp/hashlistarticle.asp

Typically you get HashTable data via the Key, but it's very simple to
implement an iterator/enumerator. This should help.

Cheers,

Bill


This article doesn't show how to get an item from a HastTable by index.
That is not possible. HashTable does not even store the order in which
items were added, and there is no such thing as an index in a hashtable.
Items are stored in "buckets" which is what gives the Hashtable it's
speed. The drawback is that it cannot store an order to the items.

You can iterate any collection using an Enumerator that implements
IEnumerable. You can get an Enumerator using the "GetEnumerator()"
method on HashTable, but that does not return the items in any
particular order. And as you iterate an Enumerator you cannot modify
the collection.

What the article you provided does show is how to create a new type of
collection that has these capabilities. It also has an internal
HashTable and a separate internal ArrayList. This is the same concept as
I mentioned.

The differences:
My version inherits from CollectionBase which has the benefit of being
bindable to controls (because it implements IList). It is also strongly
typed for your custom object type that will be contained in the
collection.

HashList (from article) implements IDictionary which gives you some of
the extra properties of HashTable.

A mixture of both would be really good. I'll work on this and post a new
template for the simple code generator. This shouldn't take long. Check
the site later (see link in my signature).

--
Michael Lang, MCSD
See my .NET open source projects
http://sourceforge.net/projects/colcodegen (simple code generator)
http://sourceforge.net/projects/dbobjecter (database app code generator)
http://sourceforge.net/projects/genadonet ("generic" ADO.NET)
 
Back
Top