Click here to Skip to main content
15,039,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
if (userInput == "write file")
                {
                    Console.Write("Name: ");
                    string writeFileName = Console.ReadLine();
                    Console.Write("Contents: ");
                    string[] writeFileContents = new string[50];
                    for (int i = 0; i < writeFileContents.Length; i++)
                    {
                        writeFileContents[i] = Console.ReadLine();
                        Console.Write("Line " + i + ": ");
                        userInput = Console.ReadLine();
                        if (userInput == "y")
                        {
                            
                        }
                        else if(userInput == "n")
                        {
                            break;
                        }
                    }

I keep getting this error: "A local or parameter named 'i' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter"
and I really don't understand how to fix it.


What I have tried:

I have tried using substitution variables for "i" and I couldn't get anything to work. I tried using different if statements and still nothing.
Posted
Updated 23-Nov-20 0:07am
Comments
CHill60 23-Nov-20 5:49am
   
When you say "I have tried using substitution variables for 'i' ", do you mean you have tried to use something like
or (int ii = 0; ii < writeFileContents.Length; ii++)
                    {
                        writeFileContents[ii] = Console.ReadLine();
                        Console.Write("Line " + ii + ": ");
Unfortunately you haven't included the code above this snippet where i is originally declared
Max Qsack 23-Nov-20 6:33am
   
Yes, that's something I tried. I knew that it wouldn't change anything but I am sleep deprived and acting like an idiot lol. I also tried declaring a new variable that is equal to I inside of the loop.
CHill60 23-Nov-20 9:53am
   
If you tried using ii instead of i then it would have changed everything. That's essentially what OriginalGriff said in his solution - and you have accepted that solution as the answer to your problem :-)

1 solution

The error means what it says: a variable called i is already declared in the method, so you cannot declare another new one - or you would end up with two variables with the same name, and while the compiler wouldn't get confused between them a human reader would!

It's generally a good idea to avoid single character variable names anyway and use more descriptive ones:
C#
for (int fileLineNumber = 0; fileLineNumber < writeFileContents.Length; fileLineNumber++)
    {
    writeFileContents[fileLineNumber] = Console.ReadLine();
    Console.Write("Line " + fileLineNumber + ": ");
...
    }
It helps your code become more self-documenting, as well as more maintainable - particularly when you have multiple nested loops!
Keep single character variables for "quick values" which will be used once and can't be easily confused - such as a loop counter that doesn't actually care what loop iteration it is on:
C#
for (int i = 0; i < 16; i++)
   {
   Console.Write(inputValue & 1);
   inputValue >>= 1;
   }
   

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