Which is the right Collection for me?

  • Thread starter Thread starter =?iso-8859-1?Q?Ti=EBsto?=
  • Start date 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.
 
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.
 
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.
 
Back
Top