By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

A dictionary with many keys for the same value

P: n/a
Hi all,

I'd like to find a .NET Collection in which I will be able to set many
keys for the same object.

For example, I would like to do something like that :

Dictionary<long, stringcollection = new Dictionary<long, string>();
string myString = "Hello World";

collection.Add(0, myString);
collection.Add(myString.Lenght, myString);

Console.Write(collection.Count); // Result must be 1.

So, as you can see, I want to associate two keys to the same object.
Moreover, I don't want to add the object two times, but only one.
That's why I'd like to see collection.Count equal 1.

Someone could help me to achieve that?

Thanks for your help!

Martin

Oct 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Martin wrote:
Hi all,

I'd like to find a .NET Collection in which I will be able to set many
keys for the same object.

For example, I would like to do something like that :

Dictionary<long, stringcollection = new Dictionary<long, string>();
string myString = "Hello World";

collection.Add(0, myString);
collection.Add(myString.Lenght, myString);

Console.Write(collection.Count); // Result must be 1.

So, as you can see, I want to associate two keys to the same object.
Moreover, I don't want to add the object two times, but only one.
That's why I'd like to see collection.Count equal 1.
Your stated requirement appears contradictory since your example code does in
fact add two items. Do you mean that you want to "rewrite" the key (from 0 to
11 in this case)? In that case the technique would be to remove the item with
key == 0, then add it back with key == 11. This does of course add the item
twice, however the resulting collection.Count would be 1 as desired.

Also, I find myself wanting to ask whether you may have the key/value semantics
backward here . . . but your example is contrived in such a way that it obscures
what you are really trying to do. Can you state your overall goal more
directly? (E.g., count occurrences of strings, or whatever.)

HTH,
-rick-
Oct 12 '06 #2

P: n/a
Your stated requirement appears contradictory since your example code
does in fact add two items. Do you mean that you want to "rewrite"
the key (from 0 to 11 in this case)? In that case the technique would
be to remove the item with key == 0, then add it back with key == 11.
This does of course add the item twice, however the resulting
collection.Count would be 1 as desired.
I think he was pretty clear. The example code adds the same item twice but
with different keys. He wants the collection to contain two keys pointing
to the same item so that the isn't a one-to-one relationship between a key
and the item.

Best Regards,
Dustin Campbell
Developer Express Inc.
Oct 12 '06 #3

P: n/a
I think he was pretty clear. The example code adds the same item twice but
with different keys. He wants the collection to contain two keys pointing
to the same item so that the isn't a one-to-one relationship between a key
and the item.
Exactly. I want to access the same object with two different keys.

Oct 12 '06 #4

P: n/a
Just wondering. Why does it matter? Both object refs point to the same
object anyway, your using the same object. Is there another reason?

--
William Stacey [C# MVP]

"Martin" <ma*********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
|I think he was pretty clear. The example code adds the same item twice
but
| with different keys. He wants the collection to contain two keys
pointing
| to the same item so that the isn't a one-to-one relationship between a
key
| and the item.
|
| Exactly. I want to access the same object with two different keys.
|
Oct 12 '06 #5

P: n/a
Dustin Campbell wrote:
>Your stated requirement appears contradictory since your example code
does in fact add two items. Do you mean that you want to "rewrite"
the key (from 0 to 11 in this case)? In that case the technique would
be to remove the item with key == 0, then add it back with key == 11.
This does of course add the item twice, however the resulting
collection.Count would be 1 as desired.

I think he was pretty clear. The example code adds the same item twice
but with different keys. He wants the collection to contain two keys
pointing to the same item so that the isn't a one-to-one relationship
between a key and the item.
Having multiple keys reference the same value is easy enough - but there is an
apparent contradiction in wanting the resulting dictionary to have a count of 1.
What he really wants the count to indicate, apparently, is the number of
unique values in the collection.

Regards,
-rick-
Oct 13 '06 #6

P: n/a
Martin wrote:
>I think he was pretty clear. The example code adds the same item twice but
with different keys. He wants the collection to contain two keys pointing
to the same item so that the isn't a one-to-one relationship between a key
and the item.

Exactly. I want to access the same object with two different keys.
Which is easy enough as far as that goes. However in a dictionary your keys
have to be unique across all values - this may cause you trouble depending on
what you are actually trying to do. In your given example, for instance, you
would not be able to add a second string of the same length as "Hello World" if
you wanted the string length to be a key for both of them.

As far as your requirement regarding the collection.Count result: it seems that
you want a count of unique values in your dictionary rather than the number of
unique keys (which is in effect what the default Count property gives you).
This would be doable by defining a custom collection which exposes a property
with the desired value. You will have to maintain that count (via a custom .Add
method I would think).

(If I've understood anything about what you are after at all, that is . . . if
not, please ignore)

HTH,
-rick-
Oct 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.