A dictionary with many keys for the same value

M

Martin

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, string> collection = 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
 
R

Rick Lones

Martin said:
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, string> collection = 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-
 
D

Dustin Campbell

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
 
M

Martin

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.
 
W

William Stacey [C# MVP]

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]

|> 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.
|
 
R

Rick Lones

Dustin said:
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-
 
R

Rick Lones

Martin said:
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-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top