Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot select Main Method as Startup object in Visual Studio Express 2010?

I have written a Main method in one of my classes. My startup Object says Not set. When clicking on it nothing else shows in the dropdown menu. Why can't I select the main method to be my Startup Object? I want to run my Main Method only by pressing ctrl+F7 but when doing so, nothing happens. Below is the very short Main method that I am using.

static void Main(string[] args)
{
    Program c = new Program();
    c.consoleread();
}
like image 247
Arthur Mamou-Mani Avatar asked Aug 14 '12 10:08

Arthur Mamou-Mani


5 Answers

Restarting VS made it work!

Background: This error happened to me today in a .net 4.5.1 Service project that I wanted to change to a Console applicaiton. Changed Output type to "Console Application" and it still didn't change. I also edited the .csproj by hand.

like image 172
arviman Avatar answered Oct 22 '22 19:10

arviman


I had this trouble after moving a class into a new namespace. Try closing the solution and manually editing the .csproj file:

<PropertyGroup>
  <StartupObject>MyNamespace.MyClassWithMain</StartupObject>
</PropertyGroup>
like image 25
Anthony Hayward Avatar answered Oct 22 '22 21:10

Anthony Hayward


Your main method needs to be public, static (Shared in VB.NET) and have a specific signature: it must return either void or int, and its parameter list must be either empty or be an array of string.

If your method doesn't match these requirements, it won't be selectable as a startup object.

like image 5
Dan Puzey Avatar answered Oct 22 '22 21:10

Dan Puzey


In case of VB.NET Project,

Project -> {ProjectName} Properties -> Application

If Enable application framework is checked, you can not select the usual Main method. The error is:

Startup object must be a form when "Enable application framework" is checked.

Also, upon unchecking it, the label changes from Startup form to Startup object.

Error Dialog:

enter image description here

like image 2
BiLaL Avatar answered Oct 22 '22 19:10

BiLaL


If the class that you want is not showing up in the drop down, you can double click on the project in the Solution Explorer and add the class name after the project name. (shown in code as "myproject" and "myclass")

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<StartupObject>myproject.myclass</StartupObject>
</PropertyGroup>

</Project>
like image 1
Mohammad Mehdi Moradi Avatar answered Oct 22 '22 19:10

Mohammad Mehdi Moradi