Home All Groups Group Topic Archive Search About

Dictionary with duplicate values

Author
27 Dec 2006 6:05 PM
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.

Author
27 Dec 2006 6:18 PM
Dave Sexton
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];

Show quote
"Martin Hart" <"martin dot hartturner at gmail dot com"> wrote in message
news:eLOifGeKHHA.536@TK2MSFTNGP02.phx.gbl...
> 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.
Author
27 Dec 2006 6:27 PM
Dave Sexton
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 :)

Show quote
"Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message
news:O9XZ9NeKHHA.1248@TK2MSFTNGP02.phx.gbl...
> 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" <"martin dot hartturner at gmail dot com"> wrote in message
> news:eLOifGeKHHA.536@TK2MSFTNGP02.phx.gbl...
>> 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.
>
>
Author
28 Dec 2006 8:04 AM
Martin Hart
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...

Show quote
> 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.
Author
28 Dec 2006 8:12 AM
Dave Sexton
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];

Show quote
"Martin Hart" <"martin dot hartturner at gmail dot com"> wrote in message
news:%239tsMblKHHA.2632@TK2MSFTNGP06.phx.gbl...
> 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.
Author
28 Dec 2006 2:02 PM
Martin Hart
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.

AddThis Social Bookmark Button