NOTE #2041

    The following line was commented.

    Description

    This EWI appears in C# generated code, because a foreach variable can't be assignable, so to avoid a compilation error, the line must be commented. In VB.Net the iteration variable can be assigned so, this restriction is not applied.

    Recommendations

    Check the logic of the original vb6 function. In some cases the nothing assignation to the iteration variable is a way to stop the cycle. If this is the case use the break statement instead.

    For the given example, just remove the commented line and the EWI.

    Sample VB6

    PublicFunction TestCommentedLines(ByVal P() AsString) AsInteger

    Dim S

    Dim Count AsInteger

    ForEach S In P

    IfNot S IsNothingThen

    MsgBox(S)

    Count = Count + 1

    S = Nothing

    EndIf

    Next S

    TestCommentedLines = Count

    EndFunction

    Target VB.NET

    PublicFunction TestCommentedLines(ByRef P() AsString) AsInteger

    Dim Count AsInteger

    Dim S AsString

    ForEach S AsStringIn P

    IfNot (S IsNothing) Then

    MessageBox.Show(S, Application.ProductName)

    Count += 1

    S = Nothing

    EndIf

    Next S

    Return Count

    EndFunction

    Expected VB.NET

    This Issue doesn't appear on VB.NET Code.

    Target C#

    publicint TestCommentedLines( string[] P)

    {

    int Count = 0;

    foreach (string S in P)

    {

    if (S != null)

    {

    MessageBox.Show(S, Application.ProductName);

    Count++;

    //UPGRADE_NOTE: (2041) The following line was commented.

    //S = null;

    }

    }

    return Count;

    }

    Expected C#

    publicint TestCommentedLines( string[] P)

    {

    int Count = 0;

    foreach (string S in P)

    {

    if (S != null)

    {

    MessageBox.Show(S, Application.ProductName);

    Count++;

    }

    }

    return Count;

    }


    Download VBUC Free Trial
    Download VBUC Now

    It's time to eradicate VB6
    ROI of eradicating VB6

    8 Proven Tips for
    Planning a Successful Migration

    8 Tips for migration