?
Solved

Collections help - Hashtable/SortedList?

Posted on 2004-04-18
9
Medium Priority
?
1,048 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:Dishan Fernando
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:Dishan Fernando
ID: 10856609
Oh sorry
ArrayList
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 8

Assisted Solution

by:Dishan Fernando
Dishan Fernando earned 75 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 75 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 75 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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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