Dictionary with object for key

L

Luigi

Hi all,
I have a dictionary (C# 2.0) with an object (an instance of a class) for the
key.
The class has only a "name" field.

dictionary<object, ...>

When I use ContainsKey property of the dictionary to check if an object with
the same "name", it returns me false.

How can I check if an object with the same "name" field is already a key in
my dictionary?

Thanks a lot.
 
R

raylopez99

You need to associate a "key" with each "name". Right now it looks
like your "key" is set to 'null'.

Any book on dictionaries in C# can explain this to you.

RL
 
A

Arne Vajhøj

Luigi said:
I have a dictionary (C# 2.0) with an object (an instance of a class) for the
key.
The class has only a "name" field.

dictionary<object, ...>

When I use ContainsKey property of the dictionary to check if an object with
the same "name", it returns me false.

How can I check if an object with the same "name" field is already a key in
my dictionary?

The object needs proper HashCode and Equals methods to work as you want.

Arne
 

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