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. How to use Dictionary?

How to use Dictionary?

Scheduled Pinned Locked Moved C#
tutorialquestion
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.
  • N Offline
    N Offline
    Nothend
    wrote on last edited by
    #1

    Hi,all I want to use Dictionary<> to make a map.I create a class named ILayer.So,I write Dictionary g_LyrNameToILayer = new Dictionary(); But it was wrong,who can tell me what shoud i do? Thanks a lot!

    M 1 Reply Last reply
    0
    • N Nothend

      Hi,all I want to use Dictionary<> to make a map.I create a class named ILayer.So,I write Dictionary g_LyrNameToILayer = new Dictionary(); But it was wrong,who can tell me what shoud i do? Thanks a lot!

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      The generic Dictionary<> class has to be parameterized with two types (one for the keys and one for the values). Guessing from your variable name you must write something like this:

      Dictionary<string, ILayer> g_LyrNameToILayer = new Dictionary<string, ILayer>();

      Regards, mav -- Black holes are the places where God divided by 0...

      N 1 Reply Last reply
      0
      • M mav northwind

        The generic Dictionary<> class has to be parameterized with two types (one for the keys and one for the values). Guessing from your variable name you must write something like this:

        Dictionary<string, ILayer> g_LyrNameToILayer = new Dictionary<string, ILayer>();

        Regards, mav -- Black holes are the places where God divided by 0...

        N Offline
        N Offline
        Nothend
        wrote on last edited by
        #3

        Thanks,What you said is right,but,i want the Key is type of string,and the Value is type of pointer,It seems to be that pointer can't be a para in Dictionary. so,how shoud i do?

        L 1 Reply Last reply
        0
        • N Nothend

          Thanks,What you said is right,but,i want the Key is type of string,and the Value is type of pointer,It seems to be that pointer can't be a para in Dictionary. so,how shoud i do?

          L Offline
          L Offline
          Larantz
          wrote on last edited by
          #4

          What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.

          Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
          List<ILayer> layers = GetLayers(); //some method of propagating layers.

          int counter = 0;
          foreach(ILayer layer in layers)
          {
          layerDict.Add("Layer" + counter, layer);
          counter++;
          }

          ...
          //Retrieve a layer
          ILayer layerOne = null;

          if(layerDict.ConstainsKey("Layer1"))
          {
          layerOne = layerDict["Layer1"];
          }

          //do something with the layer.

          Best regards! -Larantz-

          for those about to code, we salute you
          http://www.tellus-software.com

          N 2 Replies Last reply
          0
          • L Larantz

            What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.

            Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
            List<ILayer> layers = GetLayers(); //some method of propagating layers.

            int counter = 0;
            foreach(ILayer layer in layers)
            {
            layerDict.Add("Layer" + counter, layer);
            counter++;
            }

            ...
            //Retrieve a layer
            ILayer layerOne = null;

            if(layerDict.ConstainsKey("Layer1"))
            {
            layerOne = layerDict["Layer1"];
            }

            //do something with the layer.

            Best regards! -Larantz-

            for those about to code, we salute you
            http://www.tellus-software.com

            N Offline
            N Offline
            Nothend
            wrote on last edited by
            #5

            Thank u very much!

            1 Reply Last reply
            0
            • L Larantz

              What mav is saying should be sufficient if you're mapping up 'Name' with an instance of 'ILayer'.

              Dictionary<string, ILayer> layerDict = new Dictionary<string, ILayer>();
              List<ILayer> layers = GetLayers(); //some method of propagating layers.

              int counter = 0;
              foreach(ILayer layer in layers)
              {
              layerDict.Add("Layer" + counter, layer);
              counter++;
              }

              ...
              //Retrieve a layer
              ILayer layerOne = null;

              if(layerDict.ConstainsKey("Layer1"))
              {
              layerOne = layerDict["Layer1"];
              }

              //do something with the layer.

              Best regards! -Larantz-

              for those about to code, we salute you
              http://www.tellus-software.com

              N Offline
              N Offline
              Nothend
              wrote on last edited by
              #6

              Thank you very much!

              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