Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Overriding a Dictionary's Item property

Overriding a Dictionary's Item property

Scheduled Pinned Locked Moved C#
helpcomtutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary

    public class DataImage : Dictionary<string,>

    which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.

    DataImage<string,> modImage = new DataImage<string,>();
    ...
    modImage["xxx"] = "yyy";

    I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to

    public override string Item[key]
    {
    get { return base[key]; }
    set
    {
    base[key] = value;
    changeOtherStuff(key, value);
    }
    }

    I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.

    Clive Pottinger Victoria, BC

    L W C 3 Replies Last reply
    0
    • C Clive D Pottinger

      Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary

      public class DataImage : Dictionary<string,>

      which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.

      DataImage<string,> modImage = new DataImage<string,>();
      ...
      modImage["xxx"] = "yyy";

      I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to

      public override string Item[key]
      {
      get { return base[key]; }
      set
      {
      base[key] = value;
      changeOtherStuff(key, value);
      }
      }

      I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.

      Clive Pottinger Victoria, BC

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      cpotting wrote:

      I also searched online, but to no avail.

      They hide that information in the documentation[^]

      led mike

      C 1 Reply Last reply
      0
      • C Clive D Pottinger

        Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary

        public class DataImage : Dictionary<string,>

        which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.

        DataImage<string,> modImage = new DataImage<string,>();
        ...
        modImage["xxx"] = "yyy";

        I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to

        public override string Item[key]
        {
        get { return base[key]; }
        set
        {
        base[key] = value;
        changeOtherStuff(key, value);
        }
        }

        I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.

        Clive Pottinger Victoria, BC

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Few observations: - when defining Item property (indexer), I believe you should provide type of the key parameter - indexer is defined using this keyword - default indexer is not marked virtual so you should use new keyword So the code should be something like the following

        public class DataImage : Dictionary<string,sometype> {
        ...
        public new sometype this[string key} {
        get {...

        Hope this helps, Mika

        The need to optimize rises from a bad design. My articles[^]

        C 1 Reply Last reply
        0
        • C Clive D Pottinger

          Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary

          public class DataImage : Dictionary<string,>

          which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.

          DataImage<string,> modImage = new DataImage<string,>();
          ...
          modImage["xxx"] = "yyy";

          I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to

          public override string Item[key]
          {
          get { return base[key]; }
          set
          {
          base[key] = value;
          changeOtherStuff(key, value);
          }
          }

          I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.

          Clive Pottinger Victoria, BC

          C Offline
          C Offline
          Clive D Pottinger
          wrote on last edited by
          #4

          sorry. The definition of DataImage should say "string,string", but the display is dropping the second "string". And for some reason, I can't get in to edit to the message.

          Clive Pottinger Victoria, BC

          1 Reply Last reply
          0
          • L led mike

            cpotting wrote:

            I also searched online, but to no avail.

            They hide that information in the documentation[^]

            led mike

            C Offline
            C Offline
            Clive D Pottinger
            wrote on last edited by
            #5

            Thank you Mike - exactly the syntax I needed. Now I can hang from ceiling and continue painting the floor...

            Clive Pottinger Victoria, BC

            1 Reply Last reply
            0
            • W Wendelius

              Few observations: - when defining Item property (indexer), I believe you should provide type of the key parameter - indexer is defined using this keyword - default indexer is not marked virtual so you should use new keyword So the code should be something like the following

              public class DataImage : Dictionary<string,sometype> {
              ...
              public new sometype this[string key} {
              get {...

              Hope this helps, Mika

              The need to optimize rises from a bad design. My articles[^]

              C Offline
              C Offline
              Clive D Pottinger
              wrote on last edited by
              #6

              Thank you too, Mika

              Clive Pottinger Victoria, BC

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups