0

this is my code:

var info;
int n = int.Parse(Console.ReadLine());
for (int a = 0; a < n; a++)
{ 
      string input = Console.ReadLine();
      List<string> inputs = input.Split(" ").ToList();
      info = new Employee(inputs[0], double.Parse(inputs[1]), inputs[2], inputs[3], inputs[4], int.Parse(inputs[5]));
}
Console.WriteLine(info.ToString());

I have to declare the variable info before the loop. The problem is that the declaring object(i dont' know how to call it)info = new Employee(inputs[0], double.Parse(inputs[1]), inputs[2], inputs[3], inputs[4], int.Parse(inputs[5])); variable is not var and the var info is highlighted by the program as a mistake. i have to change the var behind info with another variable.

Edit:

Employee info;
            int n = int.Parse(Console.ReadLine());
            for (int a = 0; a < n; a++)
            { 
                string input = Console.ReadLine();
                List<string> inputs = input.Split(" ").ToList();
                info = new Employee(inputs[0], double.Parse(inputs[1]), inputs[2], inputs[3], inputs[4], int.Parse(inputs[5]));
            }
            Console.WriteLine(info.ToString());

now that I changed the var with Employee as one of the comments suggested, i get another error in this line Console.WriteLine(info.ToString());. It highlights the info as a mistake. "Error CS0165 Use of unassigned local variable 'info'".

  • 1
    `var` only works if the type can be inferred from the assignment. But there is no assignment here. You cannot declare a variable without type (either explicitly or inferred). Here the type is `Employee` so you'd have to write `Employee info` instead of `var info`. It's not clear why you would want to loop over the input and discard everything except for the last employee though... and if that's not your intention, then you should put the console writing statement inside the loop body too, and in that case there is no reason anymore to declare the variable outside of the loop anyway. – CherryDT Oct 15 '22 at 16:54
  • If you use the keyword _var_ you should give the compiler the chance to understand what is the underlying type. If you just declare _var info_ you don't give the compiler that chance and it rejects your code. (Remember C# is statically typed language (mostly)). If you change _var_ with _dynamic_ your code will compile. See https://stackoverflow.com/questions/961581/whats-the-difference-between-dynamic-c-4-and-var – Steve Oct 15 '22 at 16:58
  • The second problem is because the program might try to access `info` before it gets assigned (that is, if `n <=0`). It's always a good idea to [search the web for the error message you get](https://www.google.com/search?q=Error+CS0165+Use+of+unassigned+local+variable+%27info%27) if you don't understand what it means. – 41686d6564 stands w. Palestine Oct 15 '22 at 17:25
  • Now, one solution to the second problem would be to declare your variable as `Employee info = null;` _**and then**_ either check that `i > 0` (otherwise, do something about it) or check that `info != null` after the loop. Alternatively, you could use `Console.WriteLine(info?.ToString());` (notice the `?` after `info`), although that's probably not very useful. – 41686d6564 stands w. Palestine Oct 15 '22 at 17:31

0 Answers0