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();
}
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.
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>
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.
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:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With