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

7

13

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();
}
Sidesaddle answered 14/8, 2012 at 10:54 Comment(5)
let us continue this discussion in chatMencher
If your project is to build a (.DLL) library Visual Studio will not allow the specification of a startup object. If this is the case, write a consuming object and refer to the library.Shardashare
After checking all C# comments below, such as adding public, I needed the BLACK MAGIC trick of clean and re-build, and closing and re-opening the project in VS (also described below).Blackman
Happy to confirm that clean and rebuild then exit VS also solved it for me... using VS 2022! Thanks @Roland, always good to know the old tricks are still the best!Ardisardisj
@NickPirocanac Great that my humble contribution from ages ago is still useful to someone. Thanks for letting me know! Thanks to SO for this stable platformBlackman
O
11

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>
Oscar answered 3/3, 2014 at 17:31 Comment(0)
B
5

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.

Bootjack answered 14/8, 2012 at 11:17 Comment(4)
Yes, but he also says he can't select the main method. This would explain why. For example, if the main method isn't static then the code will build without error, but you won't be able to select main as the startup object.Bootjack
@DanPuzey I think my main method fits all these requirements (pelase see question edit) but it is still not showing in the startup object. It does build successfuly though.Sidesaddle
If 'String[]' (with capital S) is used in place of 'string[]' (small s), such situation can arise. +1, so seems like, the Main method is getting converted to Intermediate Language and somehow the Visual Studio demands from the programmer to use what is valid for all .NET complaint languages.Diaper
I have also found that the previous namespace is shown in the "Startup object" dropdown after a clean and rebuild. Closing and re-opening VS 2010 did the trick for me.Fumble
G
4

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.

Grosvenor answered 28/12, 2015 at 11:27 Comment(3)
I edited .csproj file manually instead of looking for the new Main method in the "Startup object" option. it worked.Ladylike
Recompile project after adding static void Main(string[] args) { ... } to my class (was a .net core 3 .dll projects) and restart VS worked for meIntrusion
Recompile & restart also worked for me, no need to edit csproj file, but the restart was needed -- using Visual Studio 2022 17.1.5.Kisung
Q
2

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

Quan answered 3/1, 2018 at 13:29 Comment(0)
Q
1

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>
Quinze answered 30/7, 2020 at 21:4 Comment(0)
H
0

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

Holmberg answered 13/9, 2019 at 15:19 Comment(0)
C
0

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.

Cower answered 17/8, 2021 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.