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. Working with value pairs(a key and a value)

Working with value pairs(a key and a value)

Scheduled Pinned Locked Moved C#
helpquestion
14 Posts 8 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 CodingLover

    Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows. private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6); Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)

    I appreciate your help all the time... CodingLover :)

    N Offline
    N Offline
    N a v a n e e t h
    wrote on last edited by
    #4

    Use a generic Dictionary(TKey,TValue). It has a ContainsKey method which will return TRUE if the key supplied exists. If it exist, just update the value instead of adding new one.

    1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, you can also call Hashtable.Remove(key) before calling Hashtable.Add(key,val); there is no need to test for existence first. The same principle applies to the generic Dictionary<T> class. BTW:And it is similar to removing-then-adding a delegate to an event. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      C Offline
      C Offline
      CodingLover
      wrote on last edited by
      #5

      Thanks for replays. Actually I got data pair like One 2 and so on. I want to store each of them. If I found a duplicate one value(number) should be updated. Then later I want to take all of them to make a string. Say my data like this. One 3 Two 5 One 7 Then the string should be, One 7 Two 5

      I appreciate your help all the time... CodingLover :)

      L 1 Reply Last reply
      0
      • C CodingLover

        Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows. private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6); Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)

        I appreciate your help all the time... CodingLover :)

        T Offline
        T Offline
        TJS4u
        wrote on last edited by
        #6

        hello How can u think of storing the different value with the same "Key" As u mentioned "One"====> the key ... how it can be repeated under the same instance see link http://www.c-sharpcorner.com/UploadFile/mahesh/Hashtable11082005171748PM/Hashtable.aspx?ArticleID=6880f0d4-acc6-402c-b632-d2e353e98e62

        1 Reply Last reply
        0
        • C CodingLover

          Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows. private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6); Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)

          I appreciate your help all the time... CodingLover :)

          B Offline
          B Offline
          Brij
          wrote on last edited by
          #7

          Make a custom function for it which check whether this entry is already there or not by hshTable.ContainsKey("one") if it there then remove it as hshTable.Remove("one"); and again add as hshTable.Add("One", 6);

          Cheers!! Brij

          L 1 Reply Last reply
          0
          • B Brij

            Make a custom function for it which check whether this entry is already there or not by hshTable.ContainsKey("one") if it there then remove it as hshTable.Remove("one"); and again add as hshTable.Add("One", 6);

            Cheers!! Brij

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            Hi, create a 5-line method to do what a single statement can do? I suggest you (re)read Giorgi's and my earlier replies. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Fixturized forever. :confused:


            1 Reply Last reply
            0
            • C CodingLover

              Thanks for replays. Actually I got data pair like One 2 and so on. I want to store each of them. If I found a duplicate one value(number) should be updated. Then later I want to take all of them to make a string. Say my data like this. One 3 Two 5 One 7 Then the string should be, One 7 Two 5

              I appreciate your help all the time... CodingLover :)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #9

              Hi, you can add/replace key-value pairs as shown before. for a Dictionary<(TKey, TValue> you can enumerate all keys with foreach(TKey key in Dictionary.Keys) {...} and similar stuff goes for old Hashtables. WARNING: dictionaries, hashtables don't preserve chronology, so there is no guarantee that "One 2" will come first in the enumeration. If you insist on having that, you will need a different approach, most likely a combination of a List and a Dictionary (a List preserves order unless you instruct it to drop the order, e.g. by sorting). :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              1 Reply Last reply
              0
              • C CodingLover

                Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows. private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6); Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)

                I appreciate your help all the time... CodingLover :)

                N Offline
                N Offline
                Navneet Hegde
                wrote on last edited by
                #10

                Hi! It quite simple If Key Not Exists Keep on Adding Else If Exists RemoveKey and then Add Thanks!

                Develop2Program & Program2Develop

                T 1 Reply Last reply
                0
                • N Navneet Hegde

                  Hi! It quite simple If Key Not Exists Keep on Adding Else If Exists RemoveKey and then Add Thanks!

                  Develop2Program & Program2Develop

                  T Offline
                  T Offline
                  TJS4u
                  wrote on last edited by
                  #11

                  ohhhhhhh wht an answer u ..... made out .......... hoooooooooooo fantastic :laugh:

                  N 1 Reply Last reply
                  0
                  • T TJS4u

                    ohhhhhhh wht an answer u ..... made out .......... hoooooooooooo fantastic :laugh:

                    N Offline
                    N Offline
                    Navneet Hegde
                    wrote on last edited by
                    #12

                    ok

                    Develop2Program & Program2Develop

                    T 1 Reply Last reply
                    0
                    • C CodingLover

                      Hi all, I want to save pair of values, a key and a related value of it. I use HashTable to store them as follows. private System.Collections.Hashtable hshTable = new System.Collections.Hashtable(); hshTable.Add("One", 1); hshTable.Add("Two", 2); hshTable.Add("Three", 3); hshTable.Add("One", 6); Check that last line, I cannot do that. Actually what I want to do is, on existing entry value should be updated. How can I solve this problem. Thanks :)

                      I appreciate your help all the time... CodingLover :)

                      S Offline
                      S Offline
                      Seraph_summer
                      wrote on last edited by
                      #13

                      I think it is easy to solve your problem with many ways: e.g: use the combination of your key and value as new key;

                      1 Reply Last reply
                      0
                      • N Navneet Hegde

                        ok

                        Develop2Program & Program2Develop

                        T Offline
                        T Offline
                        TJS4u
                        wrote on last edited by
                        #14

                        OK Program2Develop & Develop2Program

                        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