4

So here's the deal, I have 2 classes at the moment (planning on adding multiple), and I get this error when i'm trying to call functions from both classes. Same namespace. I double-checked and look at my properties tab to see that it's set to compile.

using System;

namespace Game
{
public class SecondSet
{

    public void SituationSecondOne()
    {
        Console.WriteLine(" ");
        Console.WriteLine("Choices:");
        Console.WriteLine("1: First");
        Console.WriteLine("2: Second");
        Console.WriteLine(" ");

        int ChoiceOne = Convert.ToInt32(Console.ReadLine());

        switch (ChoiceOne)
        {
            case (1):
                Console.WriteLine("TEST2");
                break;
            case (2):
                Console.WriteLine("TEST2");
                break;
            case (1337):
                Console.WriteLine(" ");
                Console.WriteLine("Thank you for playing");
                Console.ReadLine();
                Environment.Exit(1);
                break;
            default:
                Console.WriteLine(" ");
                Console.WriteLine("Now, let's try that again ... (¬_¬)");
                SituationSecondOne();
                break;
        }
    }
 }
}

Now, when I call the function from the second to the first, I get no error. What type of Main() method do I need for this? (I also tried to add the original public void Main(string[] args), once added, I can no longer add public to the function I want to call to the first class)

NOTE: I added this to the first class

SecondSet s2 = new SecondSet();

And it works fine as the code is posted above, but I get the compile error mentioned. valve pls fix :/

TheEpicPebbles
  • 71
  • 1
  • 1
  • 10
  • 1
    The compiler is telling you what's wrong: you don't have a `Main` method. If you're creating an executable, you need a static `Main` method, which is called when you run the executable. If you want to run `SituationSecondOne`, you just need to make that a static `Main` method. Note that it does need to be static. (It's not clear what you mean by "I can no longer add public to the function I want to call to the first class") – Jon Skeet Jun 25 '18 at 07:09

7 Answers7

6

It's not clear what you mean.But I was looking at this issue as well, and in my case the solution was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)

All I needed to do was change the output type of the project properties to Class library

Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.

   static void Main()
   {
   }
Ali Tooshmalani
  • 241
  • 2
  • 7
  • I will add this comment to explain my situation. Maybe someone else might stumble here for same reason as me. In my case I added an empty new Angular/AspNET Core project to the project solution. Removed the "Startup.cs" file and other files not needed. On building the solution, I got this error complaining of missing "Main method suitable for entry point". Now, with @Ali Tooshmalani's suggestion to change the output to "Class Library" instead of "Console Application", the error is gone. Thank you Ali ;-) – omostan Jul 13 '20 at 06:43
  • I got this after clicking OK to update an MVC webapp project from .NET 4.0 to 4.8 (Error: "The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks") Why this is the default behavior stumps me. Can't wait to get off this garbage onto .NET 7 – CrazyPyro Mar 16 '23 at 04:53
4

You can also receive this error is you change your Main method to be async and forget to change void to be Task.

Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
1

May be your program doest not contain Main which is the entry point of console application so replace with and read this

 class Hello 
{

   public void SituationSecondOne()
{
    Console.WriteLine(" ");
    Console.WriteLine("Choices:");
    Console.WriteLine("1: First");
    Console.WriteLine("2: Second");
    Console.WriteLine(" ");

    int ChoiceOne = Convert.ToInt32(Console.ReadLine());

    switch (ChoiceOne)
    {
        case (1):
            Console.WriteLine("TEST2");
            break;
        case (2):
            Console.WriteLine("TEST2");
            break;
        case (1337):
            Console.WriteLine(" ");
            Console.WriteLine("Thank you for playing");
            Console.ReadLine();
            Environment.Exit(1);
            break;
        default:
            Console.WriteLine(" ");
            Console.WriteLine("Now, let's try that again ... (¬_¬)");
            SituationSecondOne();
            break;
    }
}
    static void Main() 
    {
        SecondSet s2 = new SecondSet();
        Console.ReadKey();
    }
}
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
1

Please follow this way and its working and resolved your error.

public  class Program{
     static async Task Main()
             { }
 }
Jagdish
  • 11
  • 1
1

This was happening to me when running my code in VS2017. Opening in VS2019 fixed the error.

async Task Main() entry point requires C# 7.1 or later, so perhaps you don't have required version installed.

Fin
  • 311
  • 1
  • 2
  • 9
0

For me it was that I set a startup project that have an Output Type (Console Application) it was the wrong project anyway, so I set the top layer as a startup project.

also changed the other layers to be of Output Type (Class Library)

iYazee6
  • 826
  • 12
  • 22
0

As OP did not restricted to WinForms, and I have this error because of a mixed WinForms => WPF approach, I feel that is OK to answer here.

In my case I have .NET Framework 4.8 that gets data from an ancient Outlook .pst file; on top of this I want to display contacts with a WPF UI - I know, I know, why not with a WinForms?

For WPF, I had to add System.Xaml, PresentationCore and PresentationFramework and of course deleted the Form1.cs and Program.cs causing the error msg "CS5001 Program does not contain a static 'Main' method suitable for an entry point" that immediately appeared.

After some SO Googling, I came here and to another thread and came up with this change at .csproj:

  <ItemGroup>
    <!--<Page Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>-->
    <!-- ^^^^^^^^ ERROR ^^^^^^^^ -->
    <!-- Above leads to an error Message "CS5001 Program does not contain a static 'Main' method suitable for an entry point"-->
    
    <!-- Below is the fix for WPF in NET Framework --> 
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    ...
  <ItemGroup>

PS: As a side note, when you unload the project and click Edit Project File and ask for Find App.Xaml with CTRL-F do you notice that - sometimes, at least - the default scope is Current Project? And as your Project is unloaded, although you are in the .csproj it does not find anything inside the current .csproj?

I lose a lot of minutes in that :( before I realize that I had to change scope to Current Document...