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. Web Development
  3. ASP.NET
  4. increase values in a loop

increase values in a loop

Scheduled Pinned Locked Moved ASP.NET
data-structureshelp
13 Posts 4 Posters 1 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.
  • E eyeseetee

    hi guys is there a way i can automatically increaes a list of numbers by 1 e.g. 1,2,3,4,5 increase each one so its 2,3,4,5,6 Should I be using an array to do this or a loop The numbers are currently stored in a listbox thanks for any help cheers :)

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    use foreach to iterate over your items from your datasource ( or direct in the items collection if there's no data source ) and add 1 to each one.

    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

    1 Reply Last reply
    0
    • E eyeseetee

      hi guys is there a way i can automatically increaes a list of numbers by 1 e.g. 1,2,3,4,5 increase each one so its 2,3,4,5,6 Should I be using an array to do this or a loop The numbers are currently stored in a listbox thanks for any help cheers :)

      S Offline
      S Offline
      Sandeep Akhare
      wrote on last edited by
      #3

      I think there is no need to have any array Just check the last number and first number and iterate through it while incraesing the value. First chech Max

      Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

      E 1 Reply Last reply
      0
      • S Sandeep Akhare

        I think there is no need to have any array Just check the last number and first number and iterate through it while incraesing the value. First chech Max

        Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

        E Offline
        E Offline
        eyeseetee
        wrote on last edited by
        #4

        Hey cheers for the replies, managed to get this to work: foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); listbox1.Items.Add(test2); } what do you think? it works but is there a better way?

        E 1 Reply Last reply
        0
        • E eyeseetee

          Hey cheers for the replies, managed to get this to work: foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); listbox1.Items.Add(test2); } what do you think? it works but is there a better way?

          E Offline
          E Offline
          eyeseetee
          wrote on last edited by
          #5

          Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)

          J S 3 Replies Last reply
          0
          • E eyeseetee

            hi guys is there a way i can automatically increaes a list of numbers by 1 e.g. 1,2,3,4,5 increase each one so its 2,3,4,5,6 Should I be using an array to do this or a loop The numbers are currently stored in a listbox thanks for any help cheers :)

            J Offline
            J Offline
            Jesse Squire
            wrote on last edited by
            #6

            If you are always following the pattern that you illustrate above, with the items sorted, you may want to consider simply removing the lowest item in the list and adding a new item. Something similar to the following code would allow you to achieve your goal without needing to loop at all.

            int topValue = Int32.Parse(myDropDown.Items[myDropDown.Items.Count - 1].Value);

            string newValue = topValue++.ToString();

            myDropDown.Items.Add(new ListItem(newValue, newValue));

            myDropDown.Items.RemoveAt(0);

            Hope that helps. :)

            --Jesse

            "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

            1 Reply Last reply
            0
            • E eyeseetee

              Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)

              J Offline
              J Offline
              Jesse Squire
              wrote on last edited by
              #7

              If you're always looking to skip the first item, then you could use a simple for loop to start at the second item in the list.

              for (int index = 1; index < myDropDown.Items.Count; index++)

              {

              string value = myDropDown.Items[index].Value;

              // Work here.

              }

              If you're looking to skip arbitrary values, you could test for them and skip past them.

              foreach (ListItem item in myDropDown.Items)

              {

              if (item.Value == "2")

              {

              continue;

              }

              // Work here.

              }

              Hope that helps. :)

              --Jesse

              "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

              E 1 Reply Last reply
              0
              • E eyeseetee

                Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)

                S Offline
                S Offline
                Sandeep Akhare
                wrote on last edited by
                #8

                There is one more way Just Remove the first 0 the index and add one item at last index that should be grater that Last but one e.g you have 2 3 4 5 6 Remove 2 and add 7 at last index 3 4 5 6 7 got it

                Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                1 Reply Last reply
                0
                • J Jesse Squire

                  If you're always looking to skip the first item, then you could use a simple for loop to start at the second item in the list.

                  for (int index = 1; index < myDropDown.Items.Count; index++)

                  {

                  string value = myDropDown.Items[index].Value;

                  // Work here.

                  }

                  If you're looking to skip arbitrary values, you could test for them and skip past them.

                  foreach (ListItem item in myDropDown.Items)

                  {

                  if (item.Value == "2")

                  {

                  continue;

                  }

                  // Work here.

                  }

                  Hope that helps. :)

                  --Jesse

                  "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

                  E Offline
                  E Offline
                  eyeseetee
                  wrote on last edited by
                  #9

                  hey Yeah I'm looking to do arbitrary values basically I want to run the loop for eahc number which is after a number the user has entered I tried the second example you put above and tried if (item.Value == "4") but it still increased every number rather then just the numbers after 4 why would this be? thanks for the help so far :)

                  J 1 Reply Last reply
                  0
                  • E eyeseetee

                    Hi OK got another q now: with the loop is there a way to set it so it only runs for a certain few numbers e.g. i have 2,3,4,5,6 in the list and I want to loop through 3,4,5,6 but NOT 2. so somehting like foreach (ListItem item in listboxact.Items > 2) any help would be great cheers :)

                    S Offline
                    S Offline
                    Sandeep Akhare
                    wrote on last edited by
                    #10

                    Check this code Might be helpfull int num= Int32.Parse(ListBox1.Items[0].Text); // i know the total number of numbers are 4 ListBox1.Items.RemoveAt(0); num = num + 4; ListBox1.Items.Add(new ListItem(num.ToString()));

                    Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                    E 1 Reply Last reply
                    0
                    • S Sandeep Akhare

                      Check this code Might be helpfull int num= Int32.Parse(ListBox1.Items[0].Text); // i know the total number of numbers are 4 ListBox1.Items.RemoveAt(0); num = num + 4; ListBox1.Items.Add(new ListItem(num.ToString()));

                      Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                      E Offline
                      E Offline
                      eyeseetee
                      wrote on last edited by
                      #11

                      I'm not sure if thats what i need basically let me explain again I have 1,2,3,4,5,6 someone wants to add the number 3 i need the number 3 to be added then the rest of the numbers including 3 increase by 1 1,2,3,4,5,6,7 I have foreach (ListItem item in listboxact.Items) { string test = item.ToString(); int i_number = Int32.Parse(test); i_number++; string test2 = i_number.ToString(); ListItem item2 = new ListItem(); listbox1.Items.Add(test2); } which increases every number by 1 is that clearer? :)

                      1 Reply Last reply
                      0
                      • E eyeseetee

                        hey Yeah I'm looking to do arbitrary values basically I want to run the loop for eahc number which is after a number the user has entered I tried the second example you put above and tried if (item.Value == "4") but it still increased every number rather then just the numbers after 4 why would this be? thanks for the help so far :)

                        J Offline
                        J Offline
                        Jesse Squire
                        wrote on last edited by
                        #12

                        For that, you'll have to do more then check against a single number. The easiest way is to eat some casting cost and compare the current value to the one that you selected. Something like:

                        int controlValue = Int32.Parse(selectedValue);

                        int currentValue;

                        foreach (ListItem item in myDropDown.Items)

                        {

                        currentValue = Int32.Parse(item.Value);

                        if (currentValue < controlValue)

                        {

                        continue;

                        }

                        // Do Work

                        }

                        will increment only numbers >= your selected value.

                        --Jesse

                        "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

                        E 1 Reply Last reply
                        0
                        • J Jesse Squire

                          For that, you'll have to do more then check against a single number. The easiest way is to eat some casting cost and compare the current value to the one that you selected. Something like:

                          int controlValue = Int32.Parse(selectedValue);

                          int currentValue;

                          foreach (ListItem item in myDropDown.Items)

                          {

                          currentValue = Int32.Parse(item.Value);

                          if (currentValue < controlValue)

                          {

                          continue;

                          }

                          // Do Work

                          }

                          will increment only numbers >= your selected value.

                          --Jesse

                          "... the internet's just a big porn library with some useful articles stuck in." - Rob Rodi

                          E Offline
                          E Offline
                          eyeseetee
                          wrote on last edited by
                          #13

                          cheers that worked thanks!! Just got to get two columns in a listbox and then figure out how to enter into database to update exisitng data but i can do that myself(hopefully) cheers!!!!!!!!! :-D

                          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