Dictionary with object for key

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    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<obje ct, ...>

    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

  • raylopez99

    #2
    Re: Dictionary with object for key

    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 <ciupazNoSpamGr a...@inwind.itw rote:
    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<obje ct, ...>
    >
    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

    Comment

    • Peter Duniho

      #3
      Re: Dictionary with object for key

      On Tue, 04 Nov 2008 05:10:00 -0800, Luigi <ciupazNoSpamGr azie@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<obje ct, ...>
      >
      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 IEqualityCompar er<TKey>
      to the Dictionary<TKey , TValueconstruct or. 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

      Comment

      • =?UTF-8?B?QXJuZSBWYWpow7hq?=

        #4
        Re: Dictionary with object for key

        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<obje ct, ...>
        >
        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

        Comment

        Working...