|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Dictionary with duplicate valuesHi:
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]; 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. 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. > > Dave:
Thanks for your help and advise. > If a "dictionary" or some hypothetical class were to contain two keys with An enumerator was what I was thinking about...> identical values, how would the object determine which value should be > returned when the key is supplied to an indexer? Show quote > It sounds like what you need is a single key with an array of values such as Yes, I like the idea. The only problem is I don't have all the values to > 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]; 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 In that case you can use a generic List instead of an array:> be added to the array at once, but I'm sure I can fix this. 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. |
|||||||||||||||||||||||