Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Collections help - Hashtable/SortedList?

Posted on 2004-04-18
9
Medium Priority
?
1,050 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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