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. calculate subitems

calculate subitems

Scheduled Pinned Locked Moved C#
question
11 Posts 5 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.
  • A Offline
    A Offline
    andredani
    wrote on last edited by
    #1

    how do i calculate subitems[4] * subitems[5] + all item in listview I tryade: Int value = 0; foreach (ListViewItem lvi in listView3.Items) { int.Parse(lvi.subtitems[4].ToString())*int.Parse(lvi.subtitems[5].ToString()) } label10.Text = value.ToString(); and i´m so wrong..!:^) do u have some suggests??:confused:

    G 1 Reply Last reply
    0
    • A andredani

      how do i calculate subitems[4] * subitems[5] + all item in listview I tryade: Int value = 0; foreach (ListViewItem lvi in listView3.Items) { int.Parse(lvi.subtitems[4].ToString())*int.Parse(lvi.subtitems[5].ToString()) } label10.Text = value.ToString(); and i´m so wrong..!:^) do u have some suggests??:confused:

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      This will not work definitely as value never changes and it is equal to zero all the time. Also, you are iterating over the items and calculating the same product. Instead you should sum them. You say that you need to calculate subitems[4] * subitems[5] but subitems of which items?

      #region signature my articles #endregion

      A 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        This will not work definitely as value never changes and it is equal to zero all the time. Also, you are iterating over the items and calculating the same product. Instead you should sum them. You say that you need to calculate subitems[4] * subitems[5] but subitems of which items?

        #region signature my articles #endregion

        A Offline
        A Offline
        andredani
        wrote on last edited by
        #3

        calculate subitems[4] * subitems[5] + (next subitem[4] * subitem[5] for each subitems[4]*[5] like this: columnheader1,columnheader2,columnheader3,columnheader4,columnheader5,columnheader6, ........................................................................................3,5........... 100 .........................................................................................3,5.......... 100 the awnser should be: 700 tnx:cool:

        S 1 Reply Last reply
        0
        • A andredani

          calculate subitems[4] * subitems[5] + (next subitem[4] * subitem[5] for each subitems[4]*[5] like this: columnheader1,columnheader2,columnheader3,columnheader4,columnheader5,columnheader6, ........................................................................................3,5........... 100 .........................................................................................3,5.......... 100 the awnser should be: 700 tnx:cool:

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          I think you are attempting to do something like... int result = 0; foreach (lvi item in items) result += item.subitem[4] * item.subitem[5]; is this what you are attempting to do? I am really confused by how you get 700 from that example. Just ignore in your next post the fact that the numbers are subitems, and pretend like they are already integers so your problem becomes a bit more clear to the rest of us. -Jeff

          A 1 Reply Last reply
          0
          • S Skippums

            I think you are attempting to do something like... int result = 0; foreach (lvi item in items) result += item.subitem[4] * item.subitem[5]; is this what you are attempting to do? I am really confused by how you get 700 from that example. Just ignore in your next post the fact that the numbers are subitems, and pretend like they are already integers so your problem becomes a bit more clear to the rest of us. -Jeff

            A Offline
            A Offline
            andredani
            wrote on last edited by
            #5

            subitem[4] is price/sides and subitem[5] are how many sides I wanna se the earned money..3,5*100 + 3,5*100 = 700 how do i pass the result to a label.text :doh::doh:

            G C 2 Replies Last reply
            0
            • A andredani

              subitem[4] is price/sides and subitem[5] are how many sides I wanna se the earned money..3,5*100 + 3,5*100 = 700 how do i pass the result to a label.text :doh::doh:

              C Offline
              C Offline
              ChrisKo 0
              wrote on last edited by
              #6

              Skippums has the answer and to put it to a label.

              Label1.Text = result.ToString();

              A 1 Reply Last reply
              0
              • A andredani

                subitem[4] is price/sides and subitem[5] are how many sides I wanna se the earned money..3,5*100 + 3,5*100 = 700 how do i pass the result to a label.text :doh::doh:

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                andredani wrote:

                how do i pass the result to a label.text

                Make a string out of the result and assign it to the Text property: label.Text = result.ToString();

                --- single minded; short sighted; long gone;

                1 Reply Last reply
                0
                • C ChrisKo 0

                  Skippums has the answer and to put it to a label.

                  Label1.Text = result.ToString();

                  A Offline
                  A Offline
                  andredani
                  wrote on last edited by
                  #8

                  do i miss something..? i don´t get it? foreach (ListViewItem item in listview3.items) { result += item.subitem[4] * item.subitem[5]; } Label12.Text = result.ToString(); is this right??:doh:

                  C 1 Reply Last reply
                  0
                  • A andredani

                    do i miss something..? i don´t get it? foreach (ListViewItem item in listview3.items) { result += item.subitem[4] * item.subitem[5]; } Label12.Text = result.ToString(); is this right??:doh:

                    C Offline
                    C Offline
                    ChrisKo 0
                    wrote on last edited by
                    #9

                    That will iterate through all the ListViewItems in listview3's items collection. With each item it will multiple the 4th subitem with the 5th subitem and add that to the result variable. Once the loop has completed, you are assigning that result variable to the Text property of the Label control. So let's assume this ( using periods for decimal <US Standard> ):

                    listview3 contains:
                    row1: blah, blah, blah, blah, 3.5, 100
                    row2: blah, blah, blah, blah, 2.5, 100

                    result = 0
                    First Iteration of loop: 3.5 * 100 = 350 : result = result + 350, so result is now 350
                    Second Iteration of loop: 2.5 * 100 = 250 : result = result + 350, so result is now 600

                    Hope that helps explain it.

                    A 1 Reply Last reply
                    0
                    • C ChrisKo 0

                      That will iterate through all the ListViewItems in listview3's items collection. With each item it will multiple the 4th subitem with the 5th subitem and add that to the result variable. Once the loop has completed, you are assigning that result variable to the Text property of the Label control. So let's assume this ( using periods for decimal <US Standard> ):

                      listview3 contains:
                      row1: blah, blah, blah, blah, 3.5, 100
                      row2: blah, blah, blah, blah, 2.5, 100

                      result = 0
                      First Iteration of loop: 3.5 * 100 = 350 : result = result + 350, so result is now 350
                      Second Iteration of loop: 2.5 * 100 = 250 : result = result + 350, so result is now 600

                      Hope that helps explain it.

                      A Offline
                      A Offline
                      andredani
                      wrote on last edited by
                      #10

                      You excplains pretty nice Chris =) int result = 0; foreach (ListViewItem lvi in listView3.Items) { result += (lvi.SubItems[4].ToString()) * (lvi.SubItems[5].ToString()); } label12.Text = result.ToString(); it says: Error1Operator '*' cannot be applied to operands of type 'string' and 'string' can´t i use * in that order? What do should i do??:sigh::sigh: -- modified at 19:56 Friday 7th September, 2007

                      S 1 Reply Last reply
                      0
                      • A andredani

                        You excplains pretty nice Chris =) int result = 0; foreach (ListViewItem lvi in listView3.Items) { result += (lvi.SubItems[4].ToString()) * (lvi.SubItems[5].ToString()); } label12.Text = result.ToString(); it says: Error1Operator '*' cannot be applied to operands of type 'string' and 'string' can´t i use * in that order? What do should i do??:sigh::sigh: -- modified at 19:56 Friday 7th September, 2007

                        S Offline
                        S Offline
                        Skippums
                        wrote on last edited by
                        #11

                        result += (float)(lvi.SubItems[4].Text) * (float)(lvi.SubItems[5].Text); And also note that result should now be a float, not an int. I didn't even consider that you were using "," as the decimal separator. I am so used to using ".", that I didn't even consider the alternative. Sorry for the confusion. -Jeff

                        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