Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I have a
C#
foreach
loop running and I want to assign values to the variable but I am unable to. It shows me an error:

cannot assign because it is a foreach iteration variable


The same code in Visual Basic works fine:

VB
For Each spline As String In Regex.Split(replicFile, vbNewLine)
           Label9.Text = spline
           Me.Refresh()
           Do While spline.EndsWith(Chr(10)) Or spline.EndsWith(Chr(13))
               spline = spline.Substring(0, spline.Length - 1)
           Loop
           Do While spline.StartsWith(Chr(10)) Or spline.StartsWith(Chr(13))
               spline = spline.Substring(1, spline.Length - 1)
           Loop


Please help.

Regards
Aman Chaurasia

What I have tried:

C#
foreach (string spline in Regex.Split(replicFile, Environment.NewLine))
			{
				Label9.Text = spline;
				this.Refresh();
				while (spline.EndsWith(('\n').ToString()) || spline.EndsWith(('\r').ToString()))
				{
					spline = spline.Substring(0, spline.Length - 1);
				}
				while (spline.StartsWith(('\n').ToString()) || spline.StartsWith(('\r').ToString()))
				{
					spline = spline.Substring(1, spline.Length - 1);
				}
Posted
Updated 31-May-18 8:46am

Use a second variable:
C#
foreach (string s in Regex.Split(replicFile, Environment.NewLine))
{
    string spline = s;
    
    Label9.Text = spline;
    this.Refresh();
    
    while (spline.EndsWith(('\n').ToString()) || spline.EndsWith(('\r').ToString()))
    {
        spline = spline.Substring(0, spline.Length - 1);
    }
    while (spline.StartsWith(('\n').ToString()) || spline.StartsWith(('\r').ToString()))
    {
        spline = spline.Substring(1, spline.Length - 1);
    }

NB: You can replace both of those while loops with a single call to String.Trim[^]:
C#
foreach (string s in Regex.Split(replicFile, Environment.NewLine))
{
    string spline = s;
    
    Label9.Text = spline;
    this.Refresh();
    
    spline = spline.Trim(new[] { '\r', '\n' });
 
Share this answer
 
You have to use an equivalent
for (int i=0; i< ;++) {}
or
while
loop if you want to manipulate the collection while you loop over it.

Foreach does not support your use case.
 
Share this answer
 
Comments
Primo Chalice 28-May-18 2:40am    
Okay, I will do so. But then how did it work fine in Visual Basic?
Dirk Bahle 28-May-18 7:19am    
Not sure why this works in VB.Net - but foreach will work as long as you do not change the collection you are iterating over - so its quit likely that you either do not change the source collection - or if you change it - the change is (implicitely?) applied after the collection is iterated over ...
F-ES Sitecore 1-Jun-18 4:31am    
VB.net is a different language, it lets you do things that c# doesn't and vice versa. Asking why vb.net lets you do something and c# doesn't is like asking why French people refer to the things they eat as "aliments" rather than "food".

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