Getting an error that has already been declared inside a while loop

I am getting a message that it ID

has already been declared, so it cannot be declared again in my loop while

. Then how do I increase my cycle?

int RealID = 100;
Console.WriteLine("Enter Number");
int ID = int.Parse(Console.ReadLine());

while( ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    int ID = int.Parse(Console.ReadLine());
}
Console.WriteLine("You entered the correct ID");

      

+3


source to share


2 answers


As the error says, don't declare it again ... just assign a new value:



while (ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    ID = int.Parse(Console.ReadLine());
}

      

+1


source


By preceding your variable with a ID

type, you are actually re-declaring it.

Reuse it instead of re-declaring it by assigning a new value to it.



int RealID = 100;
Console.WriteLine("Enter Number");
int ID = int.Parse(Console.ReadLine());

while( ID != ReadID)
{
    Console.WriteLine("Incorrect ID. Enter another number");
    ID = int.Parse(Console.ReadLine());
}
Console.WriteLine("You entered the correct ID");

      

+1


source







All Articles