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. Visual Basic
  4. Question about cicle For-Next. [modified]

Question about cicle For-Next. [modified]

Scheduled Pinned Locked Moved Visual Basic
helpquestioncss
8 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
    Hurricane3000
    wrote on last edited by
    #1

    Hi to all. I have a little problem with a cicle For-Next: Reduced sample of my code: For A = 1 To B If "Condition" = True Then B = B - 1 End If Next This apparently simple code works correctly until A is less than 50 (half cicle, then it cause an error. I understood that the problem is that the variables B is initially set to the specified value 100 and it is not updated during the cicle For. I can't use a cicle Do-loop. I can't predict the final value of B. How I can resolve the problem? Thanks

    modified on Wednesday, September 2, 2009 5:03 PM

    K 1 Reply Last reply
    0
    • H Hurricane3000

      Hi to all. I have a little problem with a cicle For-Next: Reduced sample of my code: For A = 1 To B If "Condition" = True Then B = B - 1 End If Next This apparently simple code works correctly until A is less than 50 (half cicle, then it cause an error. I understood that the problem is that the variables B is initially set to the specified value 100 and it is not updated during the cicle For. I can't use a cicle Do-loop. I can't predict the final value of B. How I can resolve the problem? Thanks

      modified on Wednesday, September 2, 2009 5:03 PM

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      I'm not sure exactly what you want your end result to be, but I'm wondering if it's not this: B = 100 For A = 1 To 100 If "Condition" = True Then B = B - 1 End If Next If you always want it to loop 100 times, just set it to 100. Or if that varies as well, just setup another variable to hold the value you are looping to. Like this: B = 100 C = 100 For A = 1 To C If "Condition" = True Then B = B - 1 End If Next

      H 1 Reply Last reply
      0
      • K Kschuler

        I'm not sure exactly what you want your end result to be, but I'm wondering if it's not this: B = 100 For A = 1 To 100 If "Condition" = True Then B = B - 1 End If Next If you always want it to loop 100 times, just set it to 100. Or if that varies as well, just setup another variable to hold the value you are looping to. Like this: B = 100 C = 100 For A = 1 To C If "Condition" = True Then B = B - 1 End If Next

        H Offline
        H Offline
        Hurricane3000
        wrote on last edited by
        #3

        Thanks for reply. Excuse me, In the sample I assumed B = 100 only for exsample, but in reality the initial value of B is variable. B rapresents the total records of a file, and initially it's unpredictable. During the cicle for these records will be reduced to an unpredictable value.

        modified on Wednesday, September 2, 2009 5:16 PM

        K 1 Reply Last reply
        0
        • H Hurricane3000

          Thanks for reply. Excuse me, In the sample I assumed B = 100 only for exsample, but in reality the initial value of B is variable. B rapresents the total records of a file, and initially it's unpredictable. During the cicle for these records will be reduced to an unpredictable value.

          modified on Wednesday, September 2, 2009 5:16 PM

          K Offline
          K Offline
          Kschuler
          wrote on last edited by
          #4

          So does this work for you? B = 100 C = 100 For A = 1 To C If "Condition" = True Then B = B - 1 End If Next Or is there a problem that I'm not understanding? If you're still having trouble, it would help us to know more about the end result your looking for.

          H 1 Reply Last reply
          0
          • K Kschuler

            So does this work for you? B = 100 C = 100 For A = 1 To C If "Condition" = True Then B = B - 1 End If Next Or is there a problem that I'm not understanding? If you're still having trouble, it would help us to know more about the end result your looking for.

            H Offline
            H Offline
            Hurricane3000
            wrote on last edited by
            #5

            The following is the code: Dim TotRecords As Integer = WptFile.GetLength(0) - 1 Dim TotRecords1 As Integer = WptFile1.GetLength(0) - 1 Dim Index, Index1 As Integer For Index1 = 1 To TotRecords1 For Index = 1 To TotRecords If WptFile1(Index1) = WptFile(Index) Then WptFile1(Index1) = Nothing Exit For End If Next If WptFile1(Index1) = Nothing Then WptFile1(Index1) = WptFile1(WptFile1.GetLength(0) - 1) ReDim Preserve WptFile1((WptFile1.GetLength(0) - 1) - 1) TotRecords1 = WptFile1.GetLength(0) - 1 End If Next TotRecords is the number of records contained in array WptFile TotRecords1 is the number of records contained in array WptFile1 Array WptFile1 may contain duplicate records of Array WptFile. I want obtain to reduce the size of Array WptFile1 after deleted from it all duplicate records. Above code is written for .Net Compact Framework devices. Thanks

            S H 2 Replies Last reply
            0
            • H Hurricane3000

              The following is the code: Dim TotRecords As Integer = WptFile.GetLength(0) - 1 Dim TotRecords1 As Integer = WptFile1.GetLength(0) - 1 Dim Index, Index1 As Integer For Index1 = 1 To TotRecords1 For Index = 1 To TotRecords If WptFile1(Index1) = WptFile(Index) Then WptFile1(Index1) = Nothing Exit For End If Next If WptFile1(Index1) = Nothing Then WptFile1(Index1) = WptFile1(WptFile1.GetLength(0) - 1) ReDim Preserve WptFile1((WptFile1.GetLength(0) - 1) - 1) TotRecords1 = WptFile1.GetLength(0) - 1 End If Next TotRecords is the number of records contained in array WptFile TotRecords1 is the number of records contained in array WptFile1 Array WptFile1 may contain duplicate records of Array WptFile. I want obtain to reduce the size of Array WptFile1 after deleted from it all duplicate records. Above code is written for .Net Compact Framework devices. Thanks

              S Offline
              S Offline
              Sonhospa
              wrote on last edited by
              #6

              Hi, did you consider using generic lists instead of arrays? They might offer you a very useful flexibility and are easy to manage. The following code shows you the dupes - and if you can define your arrays as generic lists from the start, you only need the for-next part:

              Public Sub Main()
                  Dim inArr1() As String = {"Brachiosaurus", "Amargasaurus", "Mamenchisaurus"}
                  Dim inArr2() As String = {"Tyrannosaurus", "Amargasaurus", "Brontosaurus"}
              
                  Dim arr1 As New List(Of String)(inArr1)
                  Dim arr2 As New List(Of String)(inArr2)
              
                  Dim msg As String = "List 2 contains dupes: "
              
                  For i = 0 To arr1.Count - 1
                      If arr2.Contains(arr1.Item(i)) Then
                          msg &= ", " & arr1.Item(i)
                      End If
                  Next
              
                  MsgBox(msg)
              End Sub
              

              Generic lists also offer a 'remove' method! Regards Mick :thumbsup:

              1 Reply Last reply
              0
              • H Hurricane3000

                The following is the code: Dim TotRecords As Integer = WptFile.GetLength(0) - 1 Dim TotRecords1 As Integer = WptFile1.GetLength(0) - 1 Dim Index, Index1 As Integer For Index1 = 1 To TotRecords1 For Index = 1 To TotRecords If WptFile1(Index1) = WptFile(Index) Then WptFile1(Index1) = Nothing Exit For End If Next If WptFile1(Index1) = Nothing Then WptFile1(Index1) = WptFile1(WptFile1.GetLength(0) - 1) ReDim Preserve WptFile1((WptFile1.GetLength(0) - 1) - 1) TotRecords1 = WptFile1.GetLength(0) - 1 End If Next TotRecords is the number of records contained in array WptFile TotRecords1 is the number of records contained in array WptFile1 Array WptFile1 may contain duplicate records of Array WptFile. I want obtain to reduce the size of Array WptFile1 after deleted from it all duplicate records. Above code is written for .Net Compact Framework devices. Thanks

                H Offline
                H Offline
                Hurricane3000
                wrote on last edited by
                #7

                Hi, After some test, I deciced to use normal Arrays (instead ListArrays, because the firsts are a little more fast in intensive use. Normal Arrays, also allow to re-dimension the size preserving data (ReDim Preserve). I must try to use others cicles (While..., Do...., etc.) Regards Ignazio

                H 1 Reply Last reply
                0
                • H Hurricane3000

                  Hi, After some test, I deciced to use normal Arrays (instead ListArrays, because the firsts are a little more fast in intensive use. Normal Arrays, also allow to re-dimension the size preserving data (ReDim Preserve). I must try to use others cicles (While..., Do...., etc.) Regards Ignazio

                  H Offline
                  H Offline
                  Hurricane3000
                  wrote on last edited by
                  #8

                  Thanks to all, I resolved with following code: Dim TotRecords As Integer = WptFile.GetLength(0) - 1 Dim Index As Integer Dim Index1 As Integer = 1 Do Until Index1 = WptFile1.GetLength(0) For Index = 1 To TotRecords If WptFile1(Index1) = WptFile(Index) Then WptFile1(Index1) = WptFile1(WptFile1.GetLength(0) - 1) ReDim Preserve WptFile1((WptFile1.GetLength(0) - 1) - 1) Index1 = Index1 - 1 Exit For End If Next Index1 = Index1 + 1 Loop It cuts (re-dimension) array WptFile1, after deleted all duplicated records. Regards Ignazio

                  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