views:

185

answers:

5

hi What is the win-based application developing with C# equivalence of startup module we had in vb6.0? a static class ? or what?

+2  A: 

Yes, a static class named Program with a static Main() function. It is hard to miss when you look at the code auto-generated by a project template. Open Program.cs

Hans Passant
It could be somewhere else. In WPF projects you won't even find it in the generated code.
Martinho Fernandes
It's there, obj\Debug\App.g.i.cs, App.Main(). Any .NET exe must have a static Main() method, the class name doesn't matter.
Hans Passant
Yes, it has to be somewhere else. I was saying it is easy to miss sometimes and it's not always in Program.
Martinho Fernandes
A: 

static void Main() within any static class, but you can change it, as you could change entry point in VC6.

alemjerus
There's not "static class" requirement. Or even a "class" requirement.
Martinho Fernandes
How can you have a static C# method without a class?
ProfK
@ProfK: You can put it in a value type (struct).
Martinho Fernandes
+2  A: 

it is static void Main() {}, which can be found in different places depending on what type of app it is e.g.:

Winforms - it is usually found in Program.cs
WPF - it is usually found in App.cs

Mark Pearl
+5  A: 

In the properties page under "Application" there is a setting called "Startup object" which can be used to set the object with a static method named Main that will be called. This is a simple UI on the C# compiler's /main switch.

plinth
+1  A: 

too long for a comment

@odiseh

You almost certainly are talking about a Windows Forms project : and most of the comments here did /are going to/ assume you are using Visual Studio, as I will. If you are using another development environment, then please let us know : that may be relevant (?).

So, create a sample WinForms project in Visual Studio : look at the Program.cs file and its structure : that's the default, as other answers here have pointed out. My guess is that : if you are "coming from" VB6, you already know what the [STAThread] attribute is about, and what "single apartment threading" is.

The default startup format embodied in "Program.cs" is not, however, the only game in town : check out ApplicationContext: MSDN .NET 3.5 ApplicationContext : note that the usages I've seen of this are pretty rare, and often exotic : if I remember correctly, Chris Sells' book on Windows Forms had an interesting ApplicationContext example.

There are some circumstances in which you may wish to modify what's happening in the default Program.cs static class : for example you can show multiple Forms, and use Application.Run() which will mean that when all the Forms you've shown are closed, your Application will be still running, but with no UI (a problem you can handle with writing code for the FormClosing events and "doing the right thing").

Even SO's very own (highly esteemed) Marc Gravell once published, on another site, an example of an interesting use of NotifyIcon with a modified Main method : some choice "early vintage" Gravell

And do please ignore the horrible "smell" coming from the bizarre example I post below of what "you can get away with" : if there were any justice in the world, that example should result in a "blue screen of death," at least.

comments :

@Martinho : you are correct when you imply there is no "static" class absolutely required for a WinForms project startup. But I must respectfully disagree with the statement there's no "class" requirement. The static method Main, imho, must be inside a Class !

A brief demonstration of what a "monstrosity" you can actually "get away with" in .NET 4.0 : this will actually compile, and run, and put up an instance of Form1 :

using System;
using System.Windows.Forms;

// your mother told you to never do this !

namespace WindowsFormsApplication7
{
    public struct Class1
    {
        public static int Main(string[] someWastedBunchOfStrings)
        {
            (new Form1()).ShowDialog();
            return 1;
        }
    }
}

Kids : please don't try the above at home : they'll put you on Ritalin or Prozac :)

BillW
You can put `Main` in a value type (struct) as you have shown yourself.
Martinho Fernandes
@Martinho +1 : you have pointed out my own inconsistency; I forgot I threw that last "perverse" twist in at the last moment, having had the "sanity" to have never tried to break to VS that way before :)
BillW