Dictionary with duplicate values

  • Thread starter Thread starter Martin Hart
  • Start date Start date
M

Martin Hart

Hi:

I need to use a Dictionary structure, but I need the key value to be
duplicable. What generic collection can I use for this?

TIA,
Martin.
 
Hi Martin,

If a "dictionary" or some hypothetical class were to contain two keys with
identical values, how would the object determine which value should be
returned when the key is supplied to an indexer?

It sounds like what you need is a single key with an array of values such as
the following, which uses the generic Dictionary class in the 2.0 Framework:

Dictionary<string, string[]> keysWithMultiValues
= new Dictionary<string, string[]>();

keysWithMultiValues.Add("a key",
new string[] { "value 1", "value 2" });

string[] values = keysWithMultiValues["a key"];

The order of the values will be maintained so you can index into them as
such:

string value1 = keysWithMultiValues["a key"][0];
string value2 = keysWithMultiValues["a key"][1];
 
Hi Martin,

Just to clear this up a bit, by "value" I meant value of the key itself, not
the value associated with the key. I think that's what you meant as well :)

--
Dave Sexton
http://davesexton.com/blog

Dave Sexton said:
Hi Martin,

If a "dictionary" or some hypothetical class were to contain two keys with
identical values, how would the object determine which value should be
returned when the key is supplied to an indexer?

It sounds like what you need is a single key with an array of values such
as the following, which uses the generic Dictionary class in the 2.0
Framework:

Dictionary<string, string[]> keysWithMultiValues
= new Dictionary<string, string[]>();

keysWithMultiValues.Add("a key",
new string[] { "value 1", "value 2" });

string[] values = keysWithMultiValues["a key"];

The order of the values will be maintained so you can index into them as
such:

string value1 = keysWithMultiValues["a key"][0];
string value2 = keysWithMultiValues["a key"][1];

--
Dave Sexton
http://davesexton.com/blog

Martin Hart said:
Hi:

I need to use a Dictionary structure, but I need the key value to be
duplicable. What generic collection can I use for this?

TIA,
Martin.
 
Dave:

Thanks for your help and advise.
If a "dictionary" or some hypothetical class were to contain two keys with
identical values, how would the object determine which value should be
returned when the key is supplied to an indexer?

An enumerator was what I was thinking about...
It sounds like what you need is a single key with an array of values such as
the following, which uses the generic Dictionary class in the 2.0 Framework:

Dictionary<string, string[]> keysWithMultiValues
= new Dictionary<string, string[]>();

keysWithMultiValues.Add("a key",
new string[] { "value 1", "value 2" });

string[] values = keysWithMultiValues["a key"];

The order of the values will be maintained so you can index into them as
such:

string value1 = keysWithMultiValues["a key"][0];
string value2 = keysWithMultiValues["a key"][1];

Yes, I like the idea. The only problem is I don't have all the values to
be added to the array at once, but I'm sure I can fix this.

Thanks again,
Martin.
 
Hi Martin,
Yes, I like the idea. The only problem is I don't have all the values to
be added to the array at once, but I'm sure I can fix this.

In that case you can use a generic List instead of an array:

Dictionary<string, List<string>> keysWithMultiValues =
new Dictionary<string, List<string>>();

keysWithMultiValues.Add("a key", new List<string>());

keysWithMultiValues["a key"].Add("value 1");
keysWithMultiValues["a key"].Add("value 2");

string value1 = keysWithMultiValues["a key"][0];
string value2 = keysWithMultiValues["a key"][1];

--
Dave Sexton
http://davesexton.com/blog

Martin Hart said:
Dave:

Thanks for your help and advise.
If a "dictionary" or some hypothetical class were to contain two keys
with identical values, how would the object determine which value should
be returned when the key is supplied to an indexer?

An enumerator was what I was thinking about...
It sounds like what you need is a single key with an array of values such
as the following, which uses the generic Dictionary class in the 2.0
Framework:

Dictionary<string, string[]> keysWithMultiValues
= new Dictionary<string, string[]>();

keysWithMultiValues.Add("a key",
new string[] { "value 1", "value 2" });

string[] values = keysWithMultiValues["a key"];

The order of the values will be maintained so you can index into them as
such:

string value1 = keysWithMultiValues["a key"][0];
string value2 = keysWithMultiValues["a key"][1];

Yes, I like the idea. The only problem is I don't have all the values to
be added to the array at once, but I'm sure I can fix this.

Thanks again,
Martin.
 
Dave:
In that case you can use a generic List instead of an array:

Yes, that's the path I have taken. Sometimes you can't see the wood for
the trees, and that's what happened to me!!

Thanks for the heads-up ;-)

Martin.
 
Back
Top