Solved

Collections help - Hashtable/SortedList?

Posted on 2004-04-18
9
1,039 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
Comment Utility
if you stored data in a Vector., you can get the element by index
vData.get(index)
??
0
 
LVL 2

Author Comment

by:coltrane2003
Comment Utility
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
Comment Utility
Oh sorry
ArrayList
0
 
LVL 8

Assisted Solution

by:dishanf
dishanf earned 25 total points
Comment Utility
Yes.. if you r using hashtable. you can put key as "1","2"...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Accepted Solution

by:
caner_elci earned 25 total points
Comment Utility
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
Comment Utility


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
Comment Utility
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
Comment Utility
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_
Comment Utility


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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

6 Experts available now in Live!

Get 1:1 Help Now