"does not contain a static 'main' method suitable for an entry point"
Asked Answered
D

10

17

I can't figure what's my wrong with my code below.

When I try to compile I get the message:

does not contain a static 'main' method suitable for an entry point.

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RandomNumberGenerator
{

public partial class Form1 : Form
{
    private const int rangeNumberMin = 1;
    private const int rangeNumberMax = 3;
    private int randomNumber;

public Form1()
{            
        randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
}

private int GenerateNumber(int min,int max)
    {
        Random random = new Random();
        return random.Next(min, max);
    }

private void Display(object sender, EventArgs e)
    {                       
        switch (randomNumber)
        {
            case 1:
            MessageBox.Show("A");
            break;
            case 2:
            MessageBox.Show("B");
            break;
            case 3:
            MessageBox.Show("C");
            break;
        }

    }           
}
}

Can someone please tell me where I've gone wrong.

Disloyalty answered 13/6, 2013 at 19:14 Comment(4)
Random random = new Random(); return random.Next(min, max); dont new it. Make random instance top level (a class member).Lulita
Do you have a Program.cs file in your project? If not, that's what's missing.Synonymize
Where is the Program class?Glottalized
BTW, the topic if your question has nothing to do with your problem.Synonymize
S
23

Every C# program needs an entry point. By default, a new c# Windows Forms project includes a Program class in a Program.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StackOverflow6
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

You are probably missing this or deleted it.

Synonymize answered 13/6, 2013 at 19:18 Comment(0)
M
21

Your project must be created as an empty project. So the output type shows as Console Application. Change it to Class library, and it should work

Membranous answered 24/10, 2013 at 18:15 Comment(0)
U
8

simple change in code. main method should be 'Main' (Capital M).

Unimposing answered 16/2, 2016 at 6:46 Comment(0)
A
6

My case was very strange - app built fine before, but in one moment I faced with same issue.

What I had : ConsoleApplication

public static async void Main(string[] args)

so my main is async, but return type is void, but must be Task

public static async Task Main(string[] args)

Worked brilliantly before with void, not sure what was trigger this scenario, where void is became not ok to build

¯ \ (ツ) / ¯

Append answered 23/2, 2023 at 22:15 Comment(0)
P
5

Right click on the project --> Go to properties -->Change output type to "Class Library"

Prismoid answered 9/6, 2022 at 18:55 Comment(0)
U
3

I just had this issue myself.

I created a winforms project, decided to refactor my code and the project would now not contain the UI, so I deleted the Program.cs and winforms files only to get the same error you were getting.

You either need to re add the static void main() method as Matt Houser mentioned above, or go into the project properties and change the output type in the Application tab to Class Library.

Ulrikeulster answered 13/7, 2013 at 16:25 Comment(0)
M
2

I have also experienced this wrong. I changed the dropdown situated in Project properties/Application tab (Output type:). The original selected value was "Class Library" but i changed to "Windows Application" and found same error. Now resolved.

Mesoblast answered 2/5, 2014 at 18:58 Comment(0)
P
2

in the Solution Explorer: Right click on Project-->select properties-->select application tab-->select output type as Class library and save it and build it.

Psychodynamics answered 23/9, 2022 at 8:9 Comment(0)
S
1

Late, but for me, it was a "Console Application" project type, but "Build Action" in the file properties was some set to "None". Changed it to "Compile" and it was fine.

Stairs answered 2/3, 2020 at 22:9 Comment(0)
S
0

Might be you deleted Program file from your project. Just add Program.cs file your problem get resolved

Solingen answered 28/6, 2020 at 18:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.