Home > Enterprise >  How do I change the value of the string in C#?
How do I change the value of the string in C#?

Time:05-19

Playing around in C# trying to make a name input system but none of the code within my while loop affects the code outside it. First part of the code works perfectly but when entering the loop I cant manage to change any of the values which effectively traps me within it. pls help.

class name
        {
            public static string nameTag;
        }
        class oK
        {
            public static string nameBool;
        }
        static void Main(string[] args)
        {
          Console.WriteLine("Whats your Name?");
            string nameTag = Console.ReadLine();
            Console.WriteLine(nameTag   ", is that correct?");
            string nameBool = Console.ReadLine();

            if(nameBool=="no")
            {
                
                while (nameBool=="no")
                {
                    name.nameTag = String.Empty;
                    name.nameTag = Console.ReadLine();
                    Console.WriteLine(nameTag   ", is that correct?");
                    oK.nameBool = String.Empty;
                    oK.nameBool = Console.ReadLine();

                }
}
}

CodePudding user response:

In your code above, you have static classes defined when you could just be using a variable.

Here is some of your code tidied up (Notice nameTag and nameBool are only defined once):

public class Example
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Whats your Name?");
        string nameTag = Console.ReadLine();
        Console.WriteLine(nameTag   ", is that correct?");
        string nameBool = Console.ReadLine();

        while (nameBool == "no")
        {
            nameTag = Console.ReadLine();
            Console.WriteLine(nameTag   ", is that correct?");
            nameBool = Console.ReadLine();
        }
    }
}
  • Related