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. Looping

Looping

Scheduled Pinned Locked Moved C#
questiondata-structuresregexhelp
5 Posts 4 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.
  • J Offline
    J Offline
    Jim Warburton
    wrote on last edited by
    #1

    I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work

                bool test = false;
                int hold;
                int i =0;
                while (!test && i < sublist.Count)
                {
                    test = sublist\[i\].DeleteLine(new Point(e.X, e.Y));
                    if (test)
                        hold = i;
                    i++;
                }
    

    If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?

    A T L 4 Replies Last reply
    0
    • J Jim Warburton

      I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work

                  bool test = false;
                  int hold;
                  int i =0;
                  while (!test && i < sublist.Count)
                  {
                      test = sublist\[i\].DeleteLine(new Point(e.X, e.Y));
                      if (test)
                          hold = i;
                      i++;
                  }
      

      If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Try looping backwards from sublist.count to 0.

      1 Reply Last reply
      0
      • J Jim Warburton

        I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work

                    bool test = false;
                    int hold;
                    int i =0;
                    while (!test && i < sublist.Count)
                    {
                        test = sublist\[i\].DeleteLine(new Point(e.X, e.Y));
                        if (test)
                            hold = i;
                        i++;
                    }
        

        If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?

        T Offline
        T Offline
        tarasp
        wrote on last edited by
        #3

        there is nothing wrong with using break in this case, try this;

        int i=0;
        do
        {
        if (sublist[i].DeleteLine(new Point(e.X, e.Y)))
        break;
        } while (i++ < sublist.Count);

        If you are determined not to use a break you can do something like this, but I think the first example is easier to read and understand.

        int i=0;
        do { } while ( !sublist[i].DeleteLine(new Point(e.X, e.Y)) && (i++ < sublist.Count ));

        Tara

        1 Reply Last reply
        0
        • J Jim Warburton

          I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work

                      bool test = false;
                      int hold;
                      int i =0;
                      while (!test && i < sublist.Count)
                      {
                          test = sublist\[i\].DeleteLine(new Point(e.X, e.Y));
                          if (test)
                              hold = i;
                          i++;
                      }
          

          If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?

          T Offline
          T Offline
          tarasp
          wrote on last edited by
          #4

          try this:

          int i=0;
          do { } while ( !sublist[i].DeleteLine(new Point(e.X, e.Y)) && (i++ < sublist.Count ));

          actually I would prefer to use the break statement, it is easier to understand

          int i = 0;
          do {
          if ( sublist[i].DeleteLine(new Point(e.X, e.Y) ) )
          break;

          } while (i++ < sublist.Count);

          :)

          Tara

          1 Reply Last reply
          0
          • J Jim Warburton

            I am running through a list (named sublist in my code) of lines checking to see if any match where the user has clicked. At the moment I am using the following code which does work

                        bool test = false;
                        int hold;
                        int i =0;
                        while (!test && i < sublist.Count)
                        {
                            test = sublist\[i\].DeleteLine(new Point(e.X, e.Y));
                            if (test)
                                hold = i;
                            i++;
                        }
            

            If there is a match I will delete the line from the list, so I need to know the location where the match occurred. I would like to just be able to use i, but either I get an out of bounds error or miss the last line in the array. I have looked at do/while with the same results. With for/foreach I either go through the whole list and still need to remember where the match occurred or use break which I understand is poor coding practice in a for loop. My question is, is there a way to not have to use the variable hold with a do/while or while loop?

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

            Hi, you have an index (i), an initialisation, a continuation test, and an increment. That is called a for loop. To avoid needing the index outside the for loop, do the delete inside it, something like:

            for (int i=0; i<count; i++) {
            if (some test) {
            do whatever needs to be done to item i
            break;
            }
            }

            There is nothing wrong with break; it is intended to be used for an early termination of a loop, so it is a natural here. :)

            Luc Pattyn

            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