Link to home
Start Free TrialLog in
Avatar of coltrane2003
coltrane2003

asked on

Collections help - Hashtable/SortedList?

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!
Avatar of Dishan Fernando
Dishan Fernando
Flag of Malaysia image

if you stored data in a Vector., you can get the element by index
vData.get(index)
??
Avatar of coltrane2003
coltrane2003

ASKER

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.
Oh sorry
ArrayList
SOLUTION
Avatar of Dishan Fernando
Dishan Fernando
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!


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)