C# Script Error "Use of Unassigned variable 'line'?

83 Views Asked by At

At this time, I am developing a script that will take employee information and display the info in a particular way IN this case, I attempted to use the `.ToString' method so keep the input displayed on the same line without creating a new line I know this is fairly simple, however i'm not fimilar with the error that was displayed. Any help would be appreciated.

Sample of Code:

Console.WriteLine("********* Prompt for Employee information and create first employee **********");


            Console.WriteLine("");



            Console.WriteLine("Enter the First name: ");

            Console.Write(line.ToString());

            if (line.Contains("x"))
            {
                Console.WriteLine("Enter the Last name: ");
                line2 = Console.ReadLine();

//Rest of Code Follows Same Flow...
2

There are 2 best solutions below

0
On

It appears that you declared line like this:

string line;

Try changing it to something like this:

string line = "";
0
On

There is no variable called line in your code. And even if it exists somewhere before the code segment you posted, it does not have a value. You may have declared it but never initialized it. So it doesn't hold a reference to an actual object.

If you want to print something to the console window in a single line, you can use Console.Write()