Solved

Collections help - Hashtable/SortedList?

Posted on 2004-04-18
9
1,040 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

10 Experts available now in Live!

Get 1:1 Help Now