-2
foreach(string file in strAllFiles)
{
    foreach(char c in file)
    {
        if (c > 127)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand sqlcom = new SqlCommand("sp_ReplaceNonAsciiCharset", con);
                sqlcom.CommandType = CommandType.StoredProcedure;
                SqlParameter sqlparam = new SqlParameter();
                sqlparam.ParameterName = "@non_ascii_char";
                sqlparam.Direction = ParameterDirection.Input;
                sqlparam.SqlDbType = SqlDbType.NChar;
                sqlparam.Size = 2;
                sqlparam.Value = c;
                sqlcom.Parameters.Add(sqlparam);
                object o = sqlcom.ExecuteScalar();
                int i = file.IndexOf(c);
                file1 = file.Remove(i, 1);
                file2 = file1.Insert(i, o.ToString());
                file = file2;
            }
        }
    }
}

In the last line file=file2, I am getting the error:

cannot assign to file because it is a foreach iteration variable

puretppc
  • 3,232
  • 8
  • 38
  • 65
  • Please clearly specify what you intend to do! We cant help to solve it if you dont specify what you want to do! – Uli Köhler Jan 17 '14 at 04:16
  • 1
    Check http://stackoverflow.com/questions/3551696/why-cant-i-modify-the-loop-variable-in-a-foreach – link64 Jan 17 '14 at 04:16
  • 3
    The error is pretty self explanatory.. you're trying to assign a value to the variable that is part of the `foreach` iteration.. – Simon Whitehead Jan 17 '14 at 04:16
  • Copy error message to google - almost only give answers... – Fabio Jan 17 '14 at 04:19
  • You've listed a bunch of facts. Did you want to ask a question? – Eric Lippert Jan 17 '14 at 06:43
  • Funny how this downvoted-below-zero' question is the google featured response for the actual error message (google query: Cannot pass as a ref or out argument because it is a 'foreach iteration variable') – Legolas Oct 25 '17 at 16:23

3 Answers3

3

Its because you are changing the loop variable which is not allowed. One option is to add file1 to a separate list or if you use "for" loop instead of foreach you can modify it.

Adarsh Shah
  • 6,755
  • 2
  • 25
  • 39
2
  1. If you're trying to modify strAllFiles collection you probably should use for instead of foreach:
  2. Don't try to modify file in foreach(char c in file)

Something like

for(int i=0; i < strAllFiles.Count; i++) 
{     
    var file = strAllFiles[i];    
    var newFileValue = file;    
    foreach(char c in file) 
    {
      if (c > 127)
      {
         using (SqlConnection con = new SqlConnection(CS))
         {

           ...
           // modify newFileValue
           newFileValue = file2;

         } 
      }
    }
    // Modify collection
    strAllFiles[i] = newFileValue; 
}
AlexS
  • 136
  • 4
1

This statement

file = file2;

needs to be changed. As you cannot assign value to the variable that you are looping on in foreach. Perhaps, assign it to some new variable. The iteration variable is read-only and cannot be altered. From Section 8.4.4 of c# language specification.

The iteration variable corresponds to a read-only local variable with a scope that extends over the embedded statement. During execution of a foreach statement, the iteration variable represents the collection element for which an iteration is currently being performed. A compile-time error occurs if the embedded statement attempts to modify the iteration variable (by assignment or the ++ and -- operators) or pass the iteration variable as a ref or out parameter.

Ehsan
  • 31,833
  • 6
  • 56
  • 65