473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Dictionary with object for key

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

Nov 4 '08 #1
3 16937
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

On Nov 4, 5:10*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
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.
--
Luigi
Nov 4 '08 #2
On Tue, 04 Nov 2008 05:10:00 -0800, Luigi <ci****************@inwind.it>
wrote:
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?
That depends.

Do you really want/need to use your object itself as the key for the
dictionary? If not, the solution is simple: just use the "name" as the
key, rather than the whole object.

If you have to use the object instance itself as the key, then the next
question is: do you want/expect for multiple instances of your key object
to be considered the same key when the "name" field is the same? If so
then one solution is to override Equals() and GetHashCode() in your
object's class. In those methods, just call the appropriate method
(Equals() and GetHashCode()) on the field in the two objects.

Another solution is to pass an implementation of IEqualityComparer<TKey>
to the Dictionary<TKey, TValueconstructor. See
http://msdn.microsoft.com/en-us/library/ms132072.aspx for more details.

If, on the other hand, you want to continue using your key object as it is
currently (that is...different instances are considered different keys),
then you'll simply have to enumerate the dictionary elements one by one,
comparing the "name" field until you find a match.

Pete
Nov 4 '08 #3
Luigi wrote:
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
Nov 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Johnny Klunk | last post by:
Wondering if someone can give me a hand with something that I'm sure is really easy - but damned if I know what I'm doing wrong. I'm trying to read the contents of a database into an ASP...
2
by: | last post by:
Ive created an ASP class that uses a dictionary object which is filled from a recordset. It passes the object to the propterty of another ASP class byref: Public Property Let dicReplaceVars(byref...
8
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
1
by: boohoo | last post by:
I can't seem to do this: I want to take a query string and place two halves of the querystring into two separate dictionary objects. So... I loop through the collection of querystring items,...
5
by: Shahid Juma | last post by:
Hi, I was wondering before I proceed much further whether it is possible to pass in the Dictionary object (Scripting.Dictionary) from ASP to a stored procedure in SQL Server. Any sample sites or...
5
by: TWiSTeD ViBE | last post by:
Hi, While pouring over some code I've discovered a previous developer heavily uses the "dictionary" object. Whilst I see some of the advantages of using this system It's something I've not used...
26
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
4
by: Betina Andersen | last post by:
I have a dictionary object, then I create a new dictionary object and sets it equal to my original, then I pass the new dictionary object to a function that changes some of my values - but then my...
15
by: Dave Young | last post by:
I'm trying to replicate the behaviour of the Commerce.Dictionary object that was found in Commerce Server 3.0 By using a hashtable or creating a custom class that implements IDictionary, you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.