Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi team.

Hope you doing well,this question is in regards the startup object in C# console application.
during my project i came to a point where i have to set the startup object using code(in stead using property),but i am unable to get the solution.Please help me.

Your assistance will be highly appreciated!!!

Thanks and have a nice day ahead..

Dinesh Suthar
Posted
Comments
Ron Beyer 11-Oct-13 0:05am    
What do you mean "startup object"? Do you mean the static class/method that is executed first when the program runs?
[no name] 11-Oct-13 0:11am    
yes but not purposely a static class,a normal class
Ron Beyer 11-Oct-13 0:24am    
The method that is executed first must be static and called Main, it can be in a "normal" class, but the method must be marked static and the class is not instantiated before the method is called. You set the startup object in the project properties, it will let you select any class that has a static "Main" method.
[no name] 11-Oct-13 0:26am    
yes i can set it through my IDE but i am not supposed to use the property,i need it to be through code.
ArunRajendra 11-Oct-13 0:41am    
Any specific reason for not setting through IDE?

Just add code to your main class to select the appropriate startup object based on some set of options. Something like:
C#
static void Main(string[] args)
{
    string opt = args[0];
    switch (opt)
    {
        case "foo":
            Console.WriteLine("foo case");
            FooClass foo = new FooClass();
            foo.main();
            break;
        case "bar":
            Console.WriteLine("bar case");
            BarClass bar = new BarClass();
            bar.main();
            break;
        default:
            Console.WriteLine("No case");
            break;
    }
}
 
Share this answer
 
v4
Comments
[no name] 11-Oct-13 4:11am    
would you please elaborate what have you done over here?
i didn't understand what is the link between switch case and startup object here?
Richard MacCutchan 11-Oct-13 4:33am    
Take a look now.
:) thank you I have solved this by myself..
 
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