简体   繁体   中英

C# Assigning a new value to a variable

So I'm totally new to C#, in fact i just started learning the syntax about half an hour ago.

What i have done until now is just messing with the syntax and made an interactive input program of a few lines of code. When i was done typing my code and wanted to run the program, it gave me an error.

        Console.Write("What is your name?: ");
        string name = Console.ReadLine();    //'name' used here
        Console.WriteLine("Hello " + name);
        Console.ReadLine();

        Console.Write("How old are you?: ");
        int age = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("So you're name is " + name + " and you are " + age + " years old?");
        Console.ReadLine();

        Console.Write("Tell me, yes or no?: ");
        string answer = Console.ReadLine();


        if (answer == "no" || answer == "No")

        { 
           Console.Write("Ahh okay, what was your name again?: ");
           string name  = Console.ReadLine();    //'name' used here
        }

"A local or parameter named 'name' cannot be declared in this scope ..."

I knew the problem had to be in the last line of code, where i assigned a new value to the string 'name'. I know for sure this is possible in PHP, assigning new values to variables. As i said im totally new to C# , i would like to know if there is a way to do this?

thats because you redeclared the variable name twice. just remove the string declaration. You can't define the same variable name also in sub scopes. (between { } )

   if (answer == "no" || answer == "No")

    { 
       Console.Write("Ahh okay, what was your name again?: ");
       name = Console.ReadLine();
    }

It's simply that you cannot declare a variable twice. When you type string name you are declaring it. Try as follows:

    string name;
    Console.Write("What is your name?: ");
    name = Console.ReadLine();
    Console.WriteLine("Hello " + name);
    Console.ReadLine();

    Console.Write("How old are you?: ");
    int age = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("So you're name is " + name + " and you are " + age + " years old?");
    Console.ReadLine();

    Console.Write("Tell me, yes or no?: ");
    string answer = Console.ReadLine();


    if (answer == "no" || answer == "No")

    { 
       Console.Write("Ahh okay, what was your name again?: ");
       name  = Console.ReadLine();
    }

Hope this helps.

The problem is that you are declaring your string variable name at this line:

string **name** = Console.ReadLine();

Then, inside your if statement, you try to declare it again. If you mean to "reuse" the same variable, just remove the string portion (that tells c# you are declaring a variable of type string):

name = Console.ReadLine();

Have fun learning!

name is already declared. To assign new value you don't need to specify type of variable. So remove string before name in last line.

Try to remove "string" ?

    { 
       Console.Write("Ahh okay, what was your name again?: ");
       **name**  = Console.ReadLine();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM