Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello I have the following code.I am facing the error in the last line of the code . Error Is :- can not assign to 'file', because it is a foreach iteration variable. Please tell me how to change the code to successfully done


C#
 string[] filesHD = Directory.GetFiles(dirPath, filepattern1);
            string[] filesDT = Directory.GetFiles(dirPath, filepattern2);
            string[] filesTD = Directory.GetFiles(dirPath, filepattern3);

List<string> lstAllFiles = new List<string>();
            lstAllFiles.AddRange(filesHD);
            lstAllFiles.AddRange(filesDT);
            lstAllFiles.AddRange(filesTD);
            string[] strAllFiles = lstAllFiles.ToArray();



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;
            }
Posted
Updated 17-Jan-14 0:27am
v2

You are attempting to remove items from the foreach list, which would change the behaviour of the loop. As you don't seem to be doing anything with file1, it looks like you can safely remove this line and your loop should work then.

[Edit]I hadn't noticed the file=file2 assignation. That doesn't work either I'm afraid.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Jan-14 6:56am    
5
Just take the line out:
C#
file = file2;
You cannot change the iteration variable in any way while inside a foreach loop - it is under the compilers control what value it has at all times, not yours!

But...frankly that is some weird code you have going there.
Why on earth are you doing this by calling SQL for each and every character outside the small ASCII set? Do you have any idea how inefficient that is?
If you must do it via SQL (And personally, I wouldn't) then do the whole line as one object in SQL, don't play about with a character here, a character there.
But frankly, I'd do it in C# (not SQL at all), where it's probably one line of code for the whole string if you use Linq...
 
Share this answer
 
Comments
DEbopm 17-Jan-14 6:37am    
Hello , OriginalGriff ... how can I then Replace all non-ascii characters inside 6 text files by using similar looking Alphabets ? without hampering the line alignment of the file.
OriginalGriff 17-Jan-14 6:49am    
Well...the first thing to not is that your aren't replacing anything inside the file with your existing code: you are working on the full path to the file, which is not the same thing (and if you change anything there you are likely to "lose track" of the file itself and no be able to access it's content.
Directory.GetFiles does not read the files, it just returns the file names!
Second, you need to think a bit more about exactly what you are trying to do: unless your files are ASCII to start off with, any changes you make may well wreck them: UNICODE is not a "fixed length" character, so you need to be careful what you are doing. ASCII also contains a set of codes called Control Codes which have a value below 32 =which do not print either: instead they contain character some of which control how data is treated: CR and LF for example are line terminators, and TAB is for indentation and so forth.
There is also the fun of "not hampering the line alignment" which opens it's own can of worms...

What are you actually trying to do? (Not what is your code doing, but why are you trying to change the file content?)
Karthik_Mahalingam 17-Jan-14 6:56am    
5
Technically you overcome that using another kind of loop, fo instance a for:
C#
for ( int i=0; i<strAllFiles.Count; ++i)
{
    string file = strAllFiles[i];
    
    foreach(char c in file)
    {
        if (c > 127)
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
               //...
               strAllFiles[i] = file2;


If that really makes sense, it is up to you.
 
Share this answer
 
v4
Comments
Pete O'Hanlon 17-Jan-14 6:37am    
It's still wrong as he's modifying file in his inner loop.
CPallini 17-Jan-14 6:42am    
You are right, I've overlooked that.
However, at second sight, it doesn't look the case, to me (IndexOf and Remove shouldn't change the instance they are called on, right?).

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