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. Store Key / Value Pair

Store Key / Value Pair

Scheduled Pinned Locked Moved C#
question
3 Posts 2 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.
  • M Offline
    M Offline
    mark_w_
    wrote on last edited by
    #1

    Imagine a class with the following properties Shape //differnt shapes Colour //different colours Fill //yes or no Border //yes or no Etc etc – there could lots of different ones, maybe even a 100 Each value associated with the above properties has to have a key/value pair (eg [1] [Red]), the number will be used to submit to a 3rd party software and the text to give the option to the user. Now how should I store them? I could use a hashtable to store the options as DictonaryPairs, and then the data type of each property could be a DictonaryPair. This works in theory, but in practice it means the fill could be given [1] [Red] instead on [0] [No] So I then thought I could inherit from DictonaryPair to make ShapeDictonaryPair ColourDictonaryPair BoolDictonaryPair And then the data types could be the following ShapeDictonaryPair Shape ColourDictonaryPair Colour BoolDictonaryPair Fill BoolDictonaryPair Border Which would work, but I will end up with loads of different Pair classes, which I think I will have to. So then, would you use Hashtables or generic lists? The items in the list or hashtable will need to end up in combo boxes, with the user seeing the value and then store the pair Or any other ideas Did that make sense :sigh:

    S 1 Reply Last reply
    0
    • M mark_w_

      Imagine a class with the following properties Shape //differnt shapes Colour //different colours Fill //yes or no Border //yes or no Etc etc – there could lots of different ones, maybe even a 100 Each value associated with the above properties has to have a key/value pair (eg [1] [Red]), the number will be used to submit to a 3rd party software and the text to give the option to the user. Now how should I store them? I could use a hashtable to store the options as DictonaryPairs, and then the data type of each property could be a DictonaryPair. This works in theory, but in practice it means the fill could be given [1] [Red] instead on [0] [No] So I then thought I could inherit from DictonaryPair to make ShapeDictonaryPair ColourDictonaryPair BoolDictonaryPair And then the data types could be the following ShapeDictonaryPair Shape ColourDictonaryPair Colour BoolDictonaryPair Fill BoolDictonaryPair Border Which would work, but I will end up with loads of different Pair classes, which I think I will have to. So then, would you use Hashtables or generic lists? The items in the list or hashtable will need to end up in combo boxes, with the user seeing the value and then store the pair Or any other ideas Did that make sense :sigh:

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      You don't really need to create that many pairs - use the generic KeyValuePair and Dictionary classes instead. like

      Dictionary <int, Color > dict = new Dictionary <int, Color >
      dict.Add(1, Color.Red);
      dict.Add(2, Fill.All); // Won't compile.

      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      M 1 Reply Last reply
      0
      • S S Senthil Kumar

        You don't really need to create that many pairs - use the generic KeyValuePair and Dictionary classes instead. like

        Dictionary <int, Color > dict = new Dictionary <int, Color >
        dict.Add(1, Color.Red);
        dict.Add(2, Fill.All); // Won't compile.

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        M Offline
        M Offline
        mark_w_
        wrote on last edited by
        #3

        Looks good, but it wont work for my needs (my fault for not being detailed enough in the first post, sorry) The value must be a string, so they will all be Dictionary <int, string > The 1st post was only trying to give an example, and in the real app, the values will be things that are not represented by .net classes (I could make classes for each type, but again that means loads of classes)

        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