Reputation: 2333
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();
}
Upvotes: 13
Views: 23771
Reputation: 13047
If your project uses the "Enable application framework
" option, that would be one possibility why Sub Main
is not shown.
From https://visualstudiomagazine.com/articles/2007/10/01/enable-the-application-framework-in-vb.aspx
Q What happens when I check "Enable application framework" in my Visual Basic application? Why does my Sub Main disappear from the Startup Object dropdown...
A Visual Basic supports an "Application Framework" that makes it easier to manage the startup, shutdown, and exception management of an application. You enable or disable the application framework with a check box in the Project Properties. ...
Whether or not you use the Application Framework, Visual Basic generates a hidden My.Application class for your application. ...
The part of the My.Application class that you can't see includes a shared Sub Main as the entry point of the application. ... The reason Sub Main disappears from the startup combo box is that Visual Basic is supplying Sub Main for you.
That's just a summary of the most relevant aspects for this question but the linked article also includes some other details explaining the application framework.
Upvotes: 0
Reputation: 690
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:
Upvotes: 2
Reputation: 11
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>
Upvotes: 1
Reputation: 5486
I had the same issue after downgrading from .net core 3 preview to 2.2 - you need to install the latest version of the SDK from here:
https://dotnet.microsoft.com/download/dotnet-core/2.2
Upvotes: 0
Reputation: 5255
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.
Upvotes: 4
Reputation: 2332
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>
Upvotes: 11
Reputation: 34218
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.
Upvotes: 5