Solved

Collections help - Hashtable/SortedList?

Posted on 2004-04-18
9
1,043 Views
Last Modified: 2008-02-01
hi,

I need to store a list of strings. Then later I need be able to provide a string value and get back the index or integer position within the collection or list of strings associated with that string value.

So for example, if my list was...

cats
dogs
mice
fleas

I would be able to pass the value "mice" and get back the number 3 (or 2 if it's zero based - it doesn't matter). It also doesn't matter if the number is an actual integer or a string, because I can just cast it back. What matters is that the number represents the true position within the collection.

I was thinking I could do this with either a Hashtable or a SortedList, but I haven't figured out how. None of the methods seem to allow me to do this.

So far I have been able to add the paired values by using a counter variable cast to a string for the index, but I can't figure out how to get this value back out with the string value.

Thanks in advance!
0
Comment
Question by:coltrane2003
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:dishanf
ID: 10856493
if you stored data in a Vector., you can get the element by index
vData.get(index)
??
0
 
LVL 2

Author Comment

by:coltrane2003
ID: 10856561
what class library would that be in? I don't see vector in System.Collections

But anyways I can already get the value by index. I need to get the index by value.

I just had a thought, however, perhaps I will simply flip things around? I will simply make the string the key and the int cast as string - the value. I suppose this makes sense since I want to use the string as the key anyways.

I would be nice to have it either way.
0
 
LVL 8

Expert Comment

by:dishanf
ID: 10856609
Oh sorry
ArrayList
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 8

Assisted Solution

by:dishanf
dishanf earned 25 total points
ID: 10856615
Yes.. if you r using hashtable. you can put key as "1","2"...
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 25 total points
ID: 10857214
Use HashTable, add your string keys as key, and indexes as data.. this way, you'll be able to access the index of "mice" by just myHashTable["mice"] ...
If you ever need to search by index, it will be slow by using myHashTable.Values collection..
0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 25 total points
ID: 10861757


If the list is sorted, then you can use Array.BinarySearch();


string[] myData = {"Dog", "Cat", "Mouse", "Gopher"};

Array.Sort(myData);

// MUST BE SORTED!!!
int location = Array.BinarySearch(myData, "Cat");
0
 
LVL 3

Assisted Solution

by:wishguitar
wishguitar earned 50 total points
ID: 10862752
This would be the easiest way I can think of...

using System.Collections.Specialized;


StringCollection sc = new StringCollection();

sc.AddRange(new string [] {"Dogs","Cats","Mice","Fleas"});

int index = sc.IndexOf("Mice");

0
 
LVL 2

Author Comment

by:coltrane2003
ID: 10863721
Hi,

As I mentioned in my second post up above, I ended up using a hashtable. This was preferable to using a StringCollection because this does not require that I use a set size. (At least the example with a StringCollection had a set size, but I'm not sure this is required.)

But, since I did not specify this, I think this last answer is the best one so I will give this answer the odd number of points. Caner elcis solution is the one I implemented, but I mentioned this in my own second post.

I'm not sure whether everyone really cares about the points. I get annoyed when I spend the time to provide a suggestion and never hear back. To be fair I am splitting up the points for everyone who took the time to make a suggestion. Thanks!
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 10865100


Thanks for the points...

If you like the hashtable concept, try looking into an arraylist as well.

System.Collections.ArrayList

This is just like an array (with a numic indexer), but you "add" and "remove" elements to it (so it is a re-sizable array)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now