PawanS
PawanS

Reputation: 7193

More Than Two main Method in Visual Studio application?

In my project I am having more than two Main method with same signature. One is a winForm and other one is Console class.

How to set any one of them as entry point.

I declared [STAThread] before one main method to set entry point but It's not working?

I am using Visual Studio express 2010

Upvotes: 22

Views: 40121

Answers (8)

Neri Barakat
Neri Barakat

Reputation: 1593

You can place a Main method in every class you declare. Some programmers take advantage of this to build a small test app into each class they declare.

However, if you declare more than one Main method among the classes of your project, you’ll need to indicate to the IDE which one you would like to be the app’s entry point. To do so:

  1. With the project open in Visual Studio, select Project > [ProjectName] Properties... (where [ProjectName] is the name of your project).
  2. Select the class containing the Main method that should be the entry point from the Startup object list box.

Upvotes: 2

Shirley
Shirley

Reputation: 1

This issue could be solved easily by steps here 1. Open the solution 2 split the two classes into separate class files 3. click on the "Project" tab from main menu bar 4. Move to cursor to the Property 5. Select the desired class name you wanted to run for "Reference Path" 6. Compile the project, the error will be ride off.

Upvotes: 0

Adiii
Adiii

Reputation: 60016

you can also do by this way but i dont know how to set which main method will be entry point

 public static void Main(string args)
    {
        Console.WriteLine("this is second main method");
    }

   public static void Main(string[] args)
    {
        Program.Main("second main method");
   }

Upvotes: 0

piyush sharma
piyush sharma

Reputation: 1

You can put more then one main method in single program but program with one main method will be compiled at a time for example : Copy paste this code in editor and change name Release in first line and see the change

#define Release 
using System;
class Program
{  
#if Release==true
    public static void Main(string[] args) //for enemy
    {     
        Console.WriteLine("go to  hell");
        Console.ReadLine();
    }


#elif Release==false
    static void Main(string[] args) //for friend
    {    

         Console.WriteLine("hello ");

        Console.ReadLine();
    }

#endif 
}

Upvotes: 0

Senthil
Senthil

Reputation: 41

If program have two main method compiler get confution so run c# program put main class give

public Class one

{
public static void main()
{
System.console.writeline("One");
}

public Class two
{
public static void main()
{
System.console.writeline("Two");
}
}

run C# in console

CSC Multimain.cs /main:one

  • Output One

CSC Multimain.cs /main:two

  • Output Two

Upvotes: 4

decyclone
decyclone

Reputation: 30840

I do not think this will work. It won't even compile and complaint that the project has two main methods. There must only be one public static main method in the project. You have to rename/remove the second one.

By the way exactly why would you like to keep both of them?

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1501616

STAThread doesn't set the entry point - it forces the runtime to use a Single-Threaded Apartment for the thread which executes Main.

As Josh says, you need to set the Startup Object in project properties. If that isn't showing in VS Express, you may need to force it to show advanced build properties: Open Tools\Options and check "Show advanced build configuration" in Projects and Solutions.

Note that your application either has to be a console app or a WinForms app. It can only be built as one (per configuration, anyway). You'll either end up with a console showing when you start is as a WinForms app, or no console showing when you start it as a console app.

Have you considered putting the bulk of the logic in a class library, and then creating two wrapper applications - one WinForms and one console - which just display the relevant UI and then delegate to the class library?

Upvotes: 8

Josh
Josh

Reputation: 69252

Go into the project properties by right clicking the project in the solution explorer and click properties. On the first tab you will see a drop down list for the entry point. Select the appropriate main method.

Upvotes: 32

Related Questions