Click here to Skip to main content
15,072,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert a project from vb.net to C#

I used two different tools but I get hundreds of errors regarding foreach loops:

Schweregrad Code Beschreibung Projekt Datei Zeile Unterdrückungszustand
Fehler CS1656 "zeile_tmp" ist "foreach-Iterationsvariable". Eine Zuweisung ist daher nicht möglich.

English:
"zeile_tmp" is "foreach iteration variable". Therefore, an assignment is not possible.

Because there are more than 50 foreach loops in my vb.net project I would need an approach which allows an easy replace of the problematic code parts.

What I have tried:

foreach (string zeile_tmp in sList.Split('\n')) // vbNewLine))
            {
                // For Each zeile_tmp As String In (sList.Split(Chr(13))) 'vbNewLine))

                zeile_tmp = Strings.LTrim(zeile_tmp);
                zeile_tmp = Strings.RTrim(zeile_tmp);
                Strings.Replace(zeile_tmp, Conversions.ToString('\n'), "");
                Strings.Replace(zeile_tmp, Conversions.ToString('\r'), "");
                Strings.Replace(zeile_tmp, "  ", " ");
                if (Strings.Len(zeile_tmp) > 1)
                {
                    foreach (string strLineDup in sList2.Split('\n')) // vbNewLine))
                    {
                        // For Each strLineDup As String In (sList2.Split(Chr(13))) 'vbNewLine))

                        Strings.Replace(strLineDup, Conversions.ToString('\n'), "");
                        Strings.Replace(strLineDup, Conversions.ToString('\r'), "");
                        Strings.Replace(strLineDup, "  ", " ");
                        if (Strings.Len(strLineDup) > 2)
                        {
                            strLineDup = Strings.LTrim(strLineDup);
                            strLineDup = Strings.RTrim(strLineDup);
                            if (Strings.InStrRev(zeile_tmp, "____", Compare: (CompareMethod)1) > 0)
                                zeile_tmp = ProjectRoutines.ExtractFullPath(zeile_tmp);
                            if (Strings.InStrRev(strLineDup, "____", Compare: (CompareMethod)1) > 0)
                                Debug.Print(strLineDup);
                            if (Strings.InStrRev(strLineDup, "____", Compare: (CompareMethod)1) > 0)
                                strLineDup = ProjectRoutines.ExtractFullPath(strLineDup);
                            if (Strings.InStrRev(zeile_tmp, strLineDup, Compare: (CompareMethod)1) > 0 | Strings.InStrRev(strLineDup, zeile_tmp, Compare: (CompareMethod)1) > 0)
                            {
                                // 'If InStr(zeile_tmp, strLineDup) > 0 Or InStr(strLineDup, zeile_tmp) > 0 Then

                                // 'Console.WriteLine(zeile_tmp & vbNewLine & strLineDup)

                                // 'TopicFLCompareResultTextBox.Text = TopicFLCompareResultTextBox.Text & vbNewLine & "=== " & strLineDup
                                // 'If InStrRev(sList, strLineDup,, 1) > 0 Then TopicFLCompareResultTextBox.Text = TopicFLCompareResultTextBox.Text & vbNewLine & "=== " & strLineDup
                                // If Len(strLineDup) > 2 And InStrRev(sList, strLineDup,, 1) > 0 Or Len(strLineDup) > 2 And InStrRev(strLineDup, sList, , 1) > 0 Then sCompare = sCompare & vbNewLine & "=== " & strLineDup
                                if (Strings.Len(strLineDup) > 2 & Strings.InStrRev(sList, strLineDup, Compare: (CompareMethod)1) > 0 | Strings.Len(strLineDup) > 2 & Strings.InStrRev(strLineDup, sList, Compare: (CompareMethod)1) > 0)
                                    sDuplicates = sDuplicates + Constants.vbNewLine + "=== " + strLineDup;
                            }
                            // 'Debug.Print("If then'" & strLineDup & "'")

                            else if (Strings.Len(strLineDup) > 3)
                            {
                                if (Strings.InStrRev(sCompare, strLineDup, Compare: (CompareMethod)1) == 0 & Strings.InStrRev(sList, strLineDup, Compare: (CompareMethod)1) == 0)
                                    sCompare = sCompare + Constants.vbNewLine + "-  -  -  " + strLineDup;
                                // If InStrRev(sCompare, strLineDup,, 1) = 0 And InStrRev(sList, strLineDup,, 1) = 0 Then sCompare = sCompare & vbNewLine & "--- " & strLineDup
                                // 'If InStrRev(TopicFLCompareResultTextBox.Text, strLineDup,, 1) = 0 And InStrRev(sList, strLineDup,, 1) = 0 Then TopicFLCompareResultTextBox.Text = TopicFLCompareResultTextBox.Text & vbNewLine & "- - - " & strLineDup

                                // 'Debug.Print("Else '" & strLineDup & "'")

                            }
                        }
                    }
                }

                Application.DoEvents();
            }
Posted
Updated 20-Apr-21 23:42pm
Comments
PIEBALDconsult 21-Apr-21 10:36am
   
Best to avoid foreach when possible.

1 solution

You cannot modify the loop variable within the foreach loop body at all:
C#
                   foreach (string strLineDup in sList2.Split('\n')) // vbNewLine))
                   {
...
                           strLineDup = Strings.LTrim(strLineDup);

VB allows this, but C# does not.
The simplest solution is to create a temporary variable inside the loop and modify that instead.
C#
                   foreach (string tempLineDup in sList2.Split('\n')) // vbNewLine))
                   {
                   string strLineDup = tempLineDup;
...
                           strLineDup = Strings.LTrim(strLineDup);
   
Comments
Jo_vb.net 21-Apr-21 9:56am
   
Great!
Thank you.
OriginalGriff 21-Apr-21 10:34am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900