Which is the right Collection for me?

  • Thread starter =?iso-8859-1?Q?Ti=EBsto?=
  • Start date
?

=?iso-8859-1?Q?Ti=EBsto?=

Hi guys, I've trying to find a collection for a very common task without any luck.

I only need a collection of Key/Value objects, cause I need to check whether a key as been added and if not, get it in.
I thought a HashTable would be enough. The problem with HashTable and with SortedList is that they really "re-sort" the elements when a new element gets into the collection. So, when I loop throught the collection using a foreach or a GetEnumerator, the elements appear in a different order. I Don't want this to happen. I want to read the elements in the same order I created them. But I still need the "key search" functionally. I don't want to create a special class for this, I'm sure this is a quite simple task.

Thanks in advance.
 
B

Bob Powell [MVP]

Add the objects to two collections. One hashtable and one array list. The hashtable will give you objects for keys and the array list will store them in the order you chose.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Hi guys, I've trying to find a collection for a very common task without any luck.

I only need a collection of Key/Value objects, cause I need to check whether a key as been added and if not, get it in.
I thought a HashTable would be enough. The problem with HashTable and with SortedList is that they really "re-sort" the elements when a new element gets into the collection. So, when I loop throught the collection using a foreach or a GetEnumerator, the elements appear in a different order. I Don't want this to happen. I want to read the elements in the same order I created them. But I still need the "key search" functionally. I don't want to create a special class for this, I'm sure this is a quite simple task.

Thanks in advance.
 
?

=?iso-8859-1?Q?Ti=EBsto?=

So, none of the many collections can achieve this? Sounds strange... anyhow, I will to take the aproach you suggest, Thank you!
Add the objects to two collections. One hashtable and one array list. The hashtable will give you objects for keys and the array list will store them in the order you chose.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Hi guys, I've trying to find a collection for a very common task without any luck.

I only need a collection of Key/Value objects, cause I need to check whether a key as been added and if not, get it in.
I thought a HashTable would be enough. The problem with HashTable and with SortedList is that they really "re-sort" the elements when a new element gets into the collection. So, when I loop throught the collection using a foreach or a GetEnumerator, the elements appear in a different order. I Don't want this to happen. I want to read the elements in the same order I created them. But I still need the "key search" functionally. I don't want to create a special class for this, I'm sure this is a quite simple task.

Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top