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. sorted list question

sorted list question

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

    Hi all I'm totaling up a list of items in a Binding source and I am just having a small problem with syntax for incrementing the value of a key if it already exists any help is greatly appreciated line 16 below [code]SortedList mySortedList = new SortedList(); foreach (DataRowView view in myBindingSource) { if ((string)view["group_name"] == "USAT-Desktop") { view["group_name"] = "TEST TEST"; } if (mySortedList.Contains(view["category_name"])== false) { mySortedList.Add(view["category_name"], 1); } else { //how do I ++ the value of the key if it already exists? //mysortedlist[view["category_name"]].value +=1? }[/code]

    L 1 Reply Last reply
    0
    • C crash893

      Hi all I'm totaling up a list of items in a Binding source and I am just having a small problem with syntax for incrementing the value of a key if it already exists any help is greatly appreciated line 16 below [code]SortedList mySortedList = new SortedList(); foreach (DataRowView view in myBindingSource) { if ((string)view["group_name"] == "USAT-Desktop") { view["group_name"] = "TEST TEST"; } if (mySortedList.Contains(view["category_name"])== false) { mySortedList.Add(view["category_name"], 1); } else { //how do I ++ the value of the key if it already exists? //mysortedlist[view["category_name"]].value +=1? }[/code]

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

      Hi,

      mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];

      should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      C 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi,

        mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];

        should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        C Offline
        C Offline
        crash893
        wrote on last edited by
        #3

        I'll give it a try sorry about the tags I always forget going from form to form this one is a little different

        C 1 Reply Last reply
        0
        • C crash893

          I'll give it a try sorry about the tags I always forget going from form to form this one is a little different

          C Offline
          C Offline
          crash893
          wrote on last edited by
          #4

          that works great I just tried it thanks so much I was close but I don't think I would have gotten that for a while

          L 1 Reply Last reply
          0
          • C crash893

            that works great I just tried it thanks so much I was close but I don't think I would have gotten that for a while

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

            you're welcome.

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            1 Reply Last reply
            0
            • L Luc Pattyn

              Hi,

              mysortedlist[view["category_name"]] = 1+(int)mysortedlist[view["category_name"]];

              should do it. BTW: the above code line was set inside <pre> </pre> tags; they work much better than [code] [/code] !

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              C Offline
              C Offline
              crash893
              wrote on last edited by
              #6

              Someone else suggested this solution

              private SortedList getTotals(BindingSource BS, string column)
              {
              SortedList list = new SortedList();
              string name;

                      foreach (DataRowView row in BS)
                      {
                          name = (string)row\[column\];
              
                          if (list.ContainsKey(name))
                          {
                              list\[name\]++;
                          }
                          else
                          {
                              list.Add(name, 1);
                          }
                      }
                      return list;
                  }
              

              Side thought is there an easy way to "strip" the key values into an array that i could feed a combobox?

              L 1 Reply Last reply
              0
              • C crash893

                Someone else suggested this solution

                private SortedList getTotals(BindingSource BS, string column)
                {
                SortedList list = new SortedList();
                string name;

                        foreach (DataRowView row in BS)
                        {
                            name = (string)row\[column\];
                
                            if (list.ContainsKey(name))
                            {
                                list\[name\]++;
                            }
                            else
                            {
                                list.Add(name, 1);
                            }
                        }
                        return list;
                    }
                

                Side thought is there an easy way to "strip" the key values into an array that i could feed a combobox?

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

                Hi, if you have .NET 2.0 or better, you can use generics such as SortedList<string,int> and that should allow you to do list[name]++; so it gives you the same base idea, a more readable source, and it eliminates the need to cast to int, so it does save some CPU cycles. I trust that is what you meant, but if so the HTML eater has swallowed it. list.Keys is the collection that holds all the key values, I guess you could feed it directly into a ComboBox through its DataSource property. If you need to know more, please read up on it either on your local MSDN that came with Visual Studio, or on http://msdn2.microsoft.com[^] :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                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