473,511 Members | 16,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Two way collection

I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Thanks
Chris
Apr 18 '06 #1
5 2217

Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?


Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.

--
Larry Lard
Replies to group please

Apr 18 '06 #2
Larry Lard wrote:
Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris
Apr 18 '06 #3
use an exhaustive search the other way around

"Chris" <no@spam.com> wrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
Larry Lard wrote:
Chris wrote:
I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?

Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris

Apr 18 '06 #4

"Jeff Dillon" <je********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
use an exhaustive search the other way around

"Chris" <no@spam.com> wrote in message
news:O5**************@TK2MSFTNGP05.phx.gbl...
Larry Lard wrote:
Chris wrote:

I need to do a mapping between two strings. I want to be able to look
up either string from the other. So if I know String A, I want to get
String B. If I know String B I want to get String A. I usually use
something like a hashtable, but that is one way. What should I use to
get a two way collection?
Nothing built-in I can think of, but building your own wouldn't be too
hard - since both 'keys' and 'values' are Strings, just inherit from
Hashtable and override Add to also insert (b, a) whenever you insert
(a, b). Unless you need to know 'which way round' A and B were
originally supplied, in which case you'd need to do something like
inherit from DictionaryBase, and maintain two Hashtables internally,
one for 'original way round' mapping and one for 'the other way round'
mapping.


I just hoped there was a way to do it w/o using two hashtables.

Thanks for the input.
Chris



If you aren't too worried about speed, you can use a StringCollection as a
base for something like the following collection class:

public class MyCollection : NameValueCollection
{
public string GetKeyByValue(string Value)
{
string[] keys = base.BaseGetAllKeys();
foreach (string key in keys) {
if (Array.IndexOf(base.GetValues(key), Value) >= 0) {
return key;
}
}
return null;
}
}

hth :)

Mythran

Apr 18 '06 #5
How about putting both:
(key=A, value=B)
and
(key=B, value=A) [if A<>B]

in the same hashtable? This way you could use the same hashtable to
lookup both ways (from A get B, from B get A)... mmm right?

-tom

Apr 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2188
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
2711
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
5719
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
11
18685
by: Pavils Jurjans | last post by:
Hello, There's some confusion about the purpose and difference between these handy classes... First, both of them are holding number of key - value pairs, right? Then, I see that there may be...
2
3157
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
6
4827
by: Michael D. Ober | last post by:
In VB 6, the loop iterator v in the following code must be a variant. dim v as variant dim c as new collection for each v in collection ... next v What is the general translation in VB 7.1...
3
2143
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
10
2749
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
6
13373
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
6
7191
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.