Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
i have created a program in which two entry point i main two main method but i dont know how can i execute program based on selected Main Method.
Please Help..
Posted

As Richard already pointed out, an application should have a unique entry-point; said that, you can have two different classes that exposes a method with the right signature to be an entry point, but it is not a good practice as it generates ambiguity, and you need to explicitly tell the compiler which one to use as entry-point.

Anyway, if you write something like the code snipped below:

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
   class Program1
   {
      static void Main(string[] args)
      {
      }
   }

   class Program2
   {
      static void Main(string[] args)
      {
      }
   }
}


When you try to compile the application you get an error from the compiler, because it is not able to choose the entry-point between Program1.Main and Program2.Main.
To make the application compile without errors, you should open its properties and from the combo-box named startup object you have to choose one of the two possible ones.
 
Share this answer
 
v2
This is not a legal way to put two Main Method because basically we know a program has one entry point and if a program has more than one entry point it will create confusion for Compiler. what ever come to your question you can do it by two way

If you are using VS Editor then you can follow the given steps

In Solution Explorer right click on Your Root Project Name
Select Properties
select Application Tab
select startup object from startup object Combobox
and then Compile Program.


and if you are using Command Prompt then you can compile in this way

csc FileName.cs/main:className
 
Share this answer
 
Comments
MDNadeemAkhter 27-Oct-10 12:05pm    
good one..
ShilpaKumari 27-Oct-10 12:17pm    
First way is easy way thanks
Have a single Main method and from there call either of 2 Pseudo-Main methods, perhaps with the same method signature. That would be the easiest way to achieve what you are trying to do.
 
Share this answer
 
v2
You can't have two methods with the same name, especially main(). If you want to take different paths depending on some external factor then you should add some code to main to execute one of two functions depending on whatever flag or option you have set.
 
Share this answer
 
This is a very silly question which is not legal for programmer because it is basic knowledge we canot create more than one Entry point.
a/c to me try only create one Entry point which is good in programming manner.
 
Share this answer
 
Do you mean you have two projects, each with a main and you want to select which one you run?
If so, then right click the project in the Solution Explorer and select "Set as StartUp project"
If not, then you should get a warning "Has the wrong signature to be an entry point" if it is in your Program class, and it will be ignored if it is not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900