What is the VB.NET equivalent of this C # loop for a loop?

After two years, C # my VB.NET is a little rusty. I have two lists. Let them be called originalList and targetList. Here is my C # code:

for(int i = 0; i<originalList.Count; i++)
{
    bool isMatch = false;
    foreach (string t in targetList)
    {
        if(String.Compare(originalList[i], t, true) == 0)
        {
            isMatch = true;
            break;
        }
    }
    if(isMatch)
    {
        originalList.RemoveAt(i);
        i--;
    }
}

And my VB.NET code is this:

Dim i as Integer
For i = 0 To originalList.Count - 1
    Dim isMatch as boolean = false
    For Each t As String In targetList
        If String.compare(originalList(i), t, true) = 0 Then
            isMatch = true
            Exit For
        End If
    Next

    If isMatch Then
        originalList.RemoveAt(i)
        i -= 1
    End If
Next

But with my VB.NET code, I got an index error out of range. Where am I wrong?

+3
source share
8 answers

Think about it - this is a much more elegant way to achieve what you are trying to do - and that is removing the items from your source list that appear in your target list. Consider the following lists:

Dim OriginalList As New List(Of String)(New String() {"a", "b", "c", "d"})
Dim TargetList As New List(Of String)(New String() {"a", "b", "c"})

And this is how I will remove all the elements from the original that appear in the target ...

OriginalList.RemoveAll(Function(OriginalItem) TargetList.Contains(OriginalItem))

What will be written in C #:

OriginalList.RemoveAll(OriginalItem => TargetList.Contains(OriginalItem));

, .

: . , set A B, B, A. , B, - , t - B. , B.

+20

, . # originalList.Count. VB.NET , .

MSDN: . Visual Basic start, end step , . , .

, , , , , . , , ( , ), originalList = newList, .

+14
Dim i as integer = 0
Dim t as String

For I = originalList.Count to 1 Step -1
    for each t in targetList
       if String.compare(originalList(i), t, true) = 0 then 
          originalList.RemoveAt(I)
          exit for
       end if
    next t
Next I

The For.... -1 - , , , , VB.NET.

, , . .

# ,

for(int i = originalList.Count; i<0; i--)
{
    foreach (string t in targetList)
    {
        if(String.Compare(originalList[i], t, true) == 0)
        {
                originalList.RemoveAt(i);
                break;
        }
    }
}
+7

while, :

dim i as integer = 0
While i < originalList.Count
    dim isMatch as boolean = false
    for each t as string in targetList
        if String.compare(originalList(i), t, true) = 0 then
                isMatch = true
                Exit for
        end if
    next

    if isMatch then
        originalList.RemoveAt(i)
    else
        i += 1
    end if
next
+2

, .

0

:

Dim i as Integer
For i = 0 To originalList.Count - 1
    Dim isMatch as boolean = false
    For Each t as string In targetList
        If String.compare(originalList(i), t, true) = 0 Then
            isMatch = true
            Exit For
        End If
    Next

    If isMatch Then
        originalList.RemoveAt(i)
        i -= 1
    End If
Next

originalList.Count, originalList.Count-1, .

0

, , , for. , , . , .:)

-1

, , 0, -1, , .

It seems to me that you will need to check your index account, and if you go below 0, reset, then it can start again from the beginning.

-one
source

All Articles