Go Back  Xtreme Visual Basic Talk > Other Languages > Miscellaneous Languages > C# - Main Method?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-10-2007, 06:54 AM
vbuser2006 vbuser2006 is offline
Freshman
 
Join Date: Jan 2006
Posts: 45
Default C# - Main Method?


All the tutorials I read say a program must have a Main method. I have a program which doesn't, but it compiles OK and runs fine without it. What am I missing?
Reply With Quote
  #2  
Old 05-10-2007, 07:30 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

A class library would not have an entrypoint (although you can still create a startup object for a class library) but an application absolutely must have one. The code must begin executing somewhere.

If you go into the Solution Explorer, you should see a document there named 'Program.cs'. If you open that up you should see something like the following:

Code:
namespace csTester
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
This is sort of the "code behind", if you will, that makes your startup form load.

,
Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb

Last edited by Mike Rosenblum; 05-10-2007 at 10:32 AM.
Reply With Quote
  #3  
Old 05-10-2007, 07:34 AM
Shurik12 Shurik12 is offline
Steppe Walker

Retired Moderator
* Expert *
 
Join Date: Jul 2002
Location: Ukraine/Russia/Belgium
Posts: 7,227
Default

Hi,

I'm curious. Can you show the code?
__________________
"A diaper is not like a computer that makes satisfying burbling noises from time to time, hinting at great inner complexity." Malcolm Gladwell

"I'm sitting here completely surrounded by no beer." Onslow, 'Keeping up appearances'
Reply With Quote
  #4  
Old 05-10-2007, 07:36 AM
vbuser2006 vbuser2006 is offline
Freshman
 
Join Date: Jan 2006
Posts: 45
Default

Thanks. I don't know if I looked there? I looked through
'Form1.Designer.cs' and 'Form1.cs'.

So there must be a 'Program.cs' in the solution explorer as well? I will have a look tonight.

Thanks again.

I just started using C# yesterday. As if it wasn't obvious.

Last edited by vbuser2006; 05-10-2007 at 07:39 AM. Reason: P.S
Reply With Quote
  #5  
Old 05-10-2007, 10:30 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Quote:
Originally Posted by vbuser2006 View Post
So there must be a 'Program.cs' in the solution explorer as well? I will have a look tonight.
Yep!

You won't find this in VB.NET windows application because VB.NET uses a 'Startup Form' setting within the Project Propertes page, on the Application tab. (And therefore the actual entrypoint is hidden from us.) But a VB.NET console application does use a Sub Main().

Quote:
I just started using C# yesterday. As if it wasn't obvious.
It's all good, learning's fun.
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb
Reply With Quote
  #6  
Old 05-10-2007, 01:43 PM
vbuser2006 vbuser2006 is offline
Freshman
 
Join Date: Jan 2006
Posts: 45
Red face

Quote:
Originally Posted by Mike_R View Post
Yep!

You won't find this in VB.NET windows application because VB.NET uses a 'Startup Form' setting within the Project Propertes page, on the Application tab. (And therefore the actual entrypoint is hidden from us.) But a VB.NET console application does use a Sub Main().



It's all good, learning's fun.
Let's just forget the thing I said about the main thing, it was there tucked away in the program.cs all along...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->