Why is the terminal in VS Studio Code using the wrong program?

Viewed 475

I'm sure it's a stupid question with an obvious answer, but I just can't figure it out. I'm practicing C# in VS Code, and putting all of my projects in a folder. Now when I try to test the start of my second program using "dotnet run", the terminal keeps trying to access the first project, not the one that's open.

"AdditionCalc.cs(7,17): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. [C:\Users\dylan\Desktop\C# practice\C# practice.csproj]" is the error message i get. "AdditionCalc.cs" is my first project, where I made a simple addition calculator.

using System;

namespace Practice
{
  class Calculator
  {
    static void Main(string[] args)
    {
 
 string str1;
 string str2;
 
 int number1;
 int number2;

 Console.WriteLine("Enter Number One");
    str1 = Console.ReadLine();
 Console.WriteLine("Enter Number Two");  
    str2 = Console.ReadLine();
    
     number1 = Int32.Parse(str1);
     number2 = Int32.Parse(str2);

      int answer = number1 + number2;
           
            Console.WriteLine("Those two numbers added up is: " + answer);

            string fivehundo = (answer >=500) ? "That number is bigger than 500" : "That number is smaller than 500";

            Console.WriteLine(fivehundo);

                
    }
  }
}

This is my first Project

using System;

namespace Test {   

    class NumberGuesser {
        static void Main(string[] args){

        
        
        Random rnd = new Random();
        int number = rnd.Next(1, 11);
        
        
        Console.WriteLine(number);
        
        }
    }
} 

this is my second project, where the user will guess a number and each time the console will return if the actual number is higher or lower than the one entered.

Do i specifically have to save these files in completely separate places and create a new console every time? Or is it that I have to open the file in a specific way? Help is greatly appreciated, and I know it's probably a stupid question and I'm overlooking something very basic

2 Answers

It is pretty standard to have a different folder for each project, so that would be my recommendation.

There might be an alternative way to get it to work all in one folder, but you might as well do it the typical way right from the get-go.

I'd definitely follow E.J. Brennans advice and move your code into separate folders, but I'd also add that the issue you're seeing isn't related to VS Code at all - from what you've described, you've created a new class, but not a new project.

The Terminal in VS Code is just a regular terminal session - no different than if you opened Command Prompt or Powershell on a Windows machine, or just your regular command line on Linux. When you're calling 'dotnet run', you're calling the .NET Core CLI.See the .NET doco for more information on that.

Generally speaking, 'dotnet run' with no other parameters passed to it is looking for the .csproj file in your current directory, and attempting to call a Main method that it thinks is a part of that Project. If it can see multiple Main methods but the .csproj file doesn't specify which Class's Main to use (see this question), it doesn't know what to do and throws this error for you.

Which gets us to what I think is the main problem you're facing. You've created a new file, I imagine 'NumberGuesser.cs' with the new Main method that you want to use, but you haven't actually created a new project - which would come with a new and distinct .csproj file.

I'd recommend moving your NumberGuesser code to a new project - 'dotnet new console -o NumberGuesser' from one directory level up, and then move your class into the new project directory that creates for you.

Related