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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. FOR TO NEXT - LOOP Question

FOR TO NEXT - LOOP Question

Scheduled Pinned Locked Moved Visual Basic
performancehelpquestion
4 Posts 3 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.
  • H Offline
    H Offline
    huertj
    wrote on last edited by
    #1

    Hello, does anyone know if this is a bug or if I’m doing it wrong. I’m basically I’m using two For loops to filter out items from one list if they already exits on another (for dual list control). But when I remove a item from the list and I decrement the variable it looks like it does not decrement in memory therefore the inner loop will always read 23 even though I decrement the variable Sourcecounter. SO if outer loop has 2 items and inner loop has 23 and a item matches then I decrement the initial length of the inner loop by SourceLenght - 1. But the variable Sourcecounter will always go to 23 not 22. Here is the sample Dim DestinationLenght As Integer = 2 Dim SourceLenght As Integer = 23 Dim Destinationcounter As Integer For Destinationcounter = 0 To DestinationLenght Dim Sourcecounter As Integer For Sourcecounter = 0 To SourceLenght If Me.lstSource.Items(Sourcecounter).Text.ToString = Me.lstDestination.Items(Destinationcounter).Text.ToString Then Me.lstSource.Items.RemoveAt(Sourcecounter) Sourcecounter = Sourcecounter - 1 SourceLenght = SourceLenght - 1 End If Next Next Thanks

    G A H 3 Replies Last reply
    0
    • H huertj

      Hello, does anyone know if this is a bug or if I’m doing it wrong. I’m basically I’m using two For loops to filter out items from one list if they already exits on another (for dual list control). But when I remove a item from the list and I decrement the variable it looks like it does not decrement in memory therefore the inner loop will always read 23 even though I decrement the variable Sourcecounter. SO if outer loop has 2 items and inner loop has 23 and a item matches then I decrement the initial length of the inner loop by SourceLenght - 1. But the variable Sourcecounter will always go to 23 not 22. Here is the sample Dim DestinationLenght As Integer = 2 Dim SourceLenght As Integer = 23 Dim Destinationcounter As Integer For Destinationcounter = 0 To DestinationLenght Dim Sourcecounter As Integer For Sourcecounter = 0 To SourceLenght If Me.lstSource.Items(Sourcecounter).Text.ToString = Me.lstDestination.Items(Destinationcounter).Text.ToString Then Me.lstSource.Items.RemoveAt(Sourcecounter) Sourcecounter = Sourcecounter - 1 SourceLenght = SourceLenght - 1 End If Next Next Thanks

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

      The expressions in the For statement is calculated once when the loop is started, not every iteration. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • H huertj

        Hello, does anyone know if this is a bug or if I’m doing it wrong. I’m basically I’m using two For loops to filter out items from one list if they already exits on another (for dual list control). But when I remove a item from the list and I decrement the variable it looks like it does not decrement in memory therefore the inner loop will always read 23 even though I decrement the variable Sourcecounter. SO if outer loop has 2 items and inner loop has 23 and a item matches then I decrement the initial length of the inner loop by SourceLenght - 1. But the variable Sourcecounter will always go to 23 not 22. Here is the sample Dim DestinationLenght As Integer = 2 Dim SourceLenght As Integer = 23 Dim Destinationcounter As Integer For Destinationcounter = 0 To DestinationLenght Dim Sourcecounter As Integer For Sourcecounter = 0 To SourceLenght If Me.lstSource.Items(Sourcecounter).Text.ToString = Me.lstDestination.Items(Destinationcounter).Text.ToString Then Me.lstSource.Items.RemoveAt(Sourcecounter) Sourcecounter = Sourcecounter - 1 SourceLenght = SourceLenght - 1 End If Next Next Thanks

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

        your concept will work if you use a while loop for the inner loop. Sourcecounter = 0 Sourcelength = 23 While Sourcecounter < Sourcelength Sourcecounter +=1 If <condition> then <do removal> Sourcecounter -= 1 Sourcelength -= 1 End If End While

        1 Reply Last reply
        0
        • H huertj

          Hello, does anyone know if this is a bug or if I’m doing it wrong. I’m basically I’m using two For loops to filter out items from one list if they already exits on another (for dual list control). But when I remove a item from the list and I decrement the variable it looks like it does not decrement in memory therefore the inner loop will always read 23 even though I decrement the variable Sourcecounter. SO if outer loop has 2 items and inner loop has 23 and a item matches then I decrement the initial length of the inner loop by SourceLenght - 1. But the variable Sourcecounter will always go to 23 not 22. Here is the sample Dim DestinationLenght As Integer = 2 Dim SourceLenght As Integer = 23 Dim Destinationcounter As Integer For Destinationcounter = 0 To DestinationLenght Dim Sourcecounter As Integer For Sourcecounter = 0 To SourceLenght If Me.lstSource.Items(Sourcecounter).Text.ToString = Me.lstDestination.Items(Destinationcounter).Text.ToString Then Me.lstSource.Items.RemoveAt(Sourcecounter) Sourcecounter = Sourcecounter - 1 SourceLenght = SourceLenght - 1 End If Next Next Thanks

          H Offline
          H Offline
          huertj
          wrote on last edited by
          #4

          Thanks

          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