I have an Array say: VMHArray
= (12,22,34,4) Now have another Arraylist
object say ArrayListTaskDetails
holding the data as (12,55,44,4,12,22,21,107,43,22,34) Now the below code I wrote to remove items from the list ArrayListTaskDetails
which are not present in the VMHArray
.
Code
Dim Flag : Flag = true
Dim Counter
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4
Counter = 0
Do while Flag
If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then
ArrayListTaskDetails.RemoveRange IndexSearch, 4
Flag = False
End If
Counter = Counter + 1
Loop
Next
Now Suppose a match found at IndexSearch = 0
,so according it would delete the element at locations 0,1,2,3
- quite good. But due to removal and to make the ArrayList
object contiguous by functionality other elements will be shifted by 4
to left. Now the problem is as For Loop
already incremented by 4,so it would start from next iteration from location 4
. Thus now the new element at 0 location
in the array list can never get a chance to test the equality with the VMHArray
array elements. Can we handle such scenario without loosing the intended operation or anyway if we can set the For Loop
counter to its immediate position,where the just recent match found and 4 elements removed.?
If any issue to understand the problem,let me know please!
EDIT Go to
will not work
CODE(as per @Ankit suggestion)
Option Explicit
Dim Flag : Flag = true
Dim Counter
Dim VMHArray : VMHArray = Array(12,22,34,4)
Dim ArrayListTaskDetails : Set ArrayListTaskDetails = CreateObject("System.Collections.ArrayList")
ArrayListTaskDetails.Add(45)
ArrayListTaskDetails.Add(4)
ArrayListTaskDetails.Add(22)
ArrayListTaskDetails.Add(4)
ArrayListTaskDetails.Add(45)
ArrayListTaskDetails.Add(20)
ArrayListTaskDetails.Add(12)
ArrayListTaskDetails.Add(35)
ArrayListTaskDetails.Add(34)
Restart:
For IndexSearch = 0 To ArrayListTaskDetails.Count - 1 step 4
Counter = 0
Do while Flag
If VMHArray(Counter) <> ArrayListTaskDetails (IndexSearch) Then
ArrayListTaskDetails.RemoveRange IndexSearch, 4
Goto Restart
Flag = False
End If
Counter = Counter + 1
Loop
Next
MsgBox(Join(ArrayListTaskDetails.ToArray()),"$")
Thanks,