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