Home > Mobile >  Beginner Question - Error in Starting Program in VS2022
Beginner Question - Error in Starting Program in VS2022

Time:11-27

I'm currently following the Lynda Course "Learning C Sharp" without any prior coding knowledge.

I've completed the first exercise of creating a questionnaire using commands to input and output to console, in the process learning about strings and variables.

The issue I am having is when i moved onto the next exercise and added an if statement to this questionnaire, not only did it fail to run but once i deleted the added code the original functioning program no longer works. I am unable to undo this back to before I added the code. However VS allows me to run the previous functioning program.

Additionally, as I am using VS2022 and the tutorial is using VS2019 I believe, it seems like i am not seeing the comprehensive code with the different brackets and indentations seen in the tutorial.

I'm guessing this is a function that can be turned on and that it is in this hidden formatting that something went wrong?

Would appreciate any help and knowing more about best practices to revert back to previously function code (Autosave feature? Save as periodically? etc?)

My VS2022 Code vs Tutorial VS2019 Code

// See https://aka.ms/new-console-template for more information

Console.WriteLine("What is your name?");
var name = Console.ReadLine();
Console.WriteLine("How old are you?");
var age = Console.ReadLine();
Console.WriteLine("What month were you born in?");
var month = Console.ReadLine ();
Console.WriteLine ("Your name is: {0}", name);
Console.WriteLine("Your age is: {0}", age);
Console.WriteLine("Your month of birth is: {0}", month);

Thanks!

CodePudding user response:

It looks like you created the project with a .Net 6 template for Console Applications, which allows for different syntax. Namely: you don't need all those distracting class, main, brackets ... you can just type away. It basically is "as if" the code was inside the main method of a project template you are used to.

You can either use VS 2019 with a new project or you can continue using VS2022 but set the .NET version of the project to an older one, like .Net 5 or Core 3.1 (you'll also need a new project, probably). - If you want it "ye olde way", that is.

Then for your code problem: That's what git is (also) for. You can easily make a lokal repo for the project and check in code versions that "work". Then do some changes, check in etc. If you have the need to return to a certain version of your code, you can then simply revert to that commit.

For your second Excercise, you created a new file, right? In that file, you also used that "Top-Level" Syntax. That doesn't work. It's like having two "main" functions. I suggest making a new project in the solution for each exercise.

  • Related