Section 6.3. Application Entry Points


6.3. Application Entry Points

Any Visual Basic executablea Windows Forms or Windows console applicationhas a single application-level entry point, a subroutine named Main. Main must be a method within one of the application's classes. It must also be:


A Public Routine

In VB 6, Main could be either public or privateor it didn't have to exist at all, when a form was set as the startup object. In .NET, it must be public to be visible as an entry point.


A Shared (Static) Routine

Its declaration must include the Shared keyword; this allows it to be called without the need to create an instance of its class. If Main resides in a module, it is automatically shared, even without the Shared keyword.

6.3.1. Using Main in a Standard Class or Module

The Main routine can appear in any class in your application, including a Module (which is just a Shared variation of a class). Consider the simple case of a console application, like the one shown in Example 6-1. The example includes a module named StartsHere, which contains the Main routine. At runtime, the Common Language Runtime finds the Main procedure, displays a message to the console, and then terminates the program.

Example 6-1. A simple console application

 Option Strict On Imports System Public Module StartsHere    Public Sub Main       Console.WriteLine("This is a console application.")    End Sub End Module 

Since the example uses a Module instead of a Class, the Shared keyword decoration is not necessary on the Main routine. The .NET compiler translates this code into a public class and gives it a single method, Main, as shown in the ILDASM tree diagram (see Figure 6-1).

The IL code for Main also shows that it is marked as the program's entry point (through the .enTRypoint text on the third line), and that it is a static (shared) member rather than an instance member.

         .method public static void  Main(  ) cil managed     {       .entrypoint       .custom instance void          [mscorlib]System.STAThreadAttribute::.ctor(  ) = ( 01 00 00 00 )       // Code size     11 (0xb)       .maxstack  8 

Figure 6-1. The StartsHere.Main method in ILDASM


       IL_0000:  ldstr   "This is a console application."       IL_0005:  call    void [mscorlib]System.Console::WriteLine(string)       IL_000a:  ret     } // end of method StartsHere::Main 

The Visual Basic compiler and the .NET Common Language Runtime, it would seem, have transformed this simple code module into a self-executing class.

6.3.2. Using Main in Windows Forms Applications

All forms displayed in Windows Forms applications must be instantiated before use. Visual Basic allows you to specify a Form as the startup object of an application (instead of a standard Class- or Module-based Main routine), and you don't have to add the routine yourself. So is it really there? Yes, the framework adds a shared Main routine to your class. What does it do? A quick look at the IL gives the answer. (Lines in this presentation have been wrapped for readability.)

     .method public hidebysig static void  Main(  ) cil managed     {       .entrypoint       .custom instance void          [mscorlib]System.STAThreadAttribute::.ctor(  ) = ( 01 00 00 00 )       // Code size       16 (0x10)       .maxstack  8       IL_0000:  call       class WindowsApplication1.My.MyProject/          MyForms WindowsApplication1.My.MyProject::get_Forms(  )       IL_0005:  callvirt   instance class WindowsApplication1.Form1          WindowsApplication1.My.MyProject/MyForms::get_Form1(  )       IL_000a:  call       void          [System.Windows.Forms]System.Windows.Forms.Application::Run(          class [System.Windows.Forms]System.Windows.Forms.Form)       IL_000f:  ret     } // end of method Form1::Main 

The Main routine creates an instance of the form Form1 and then calls the Run method (in the System.Windows.Forms.Application class), passing it the instance of the form. This is the normal way to run a Windows Forms application. If you want to create your own Main routine in another class that starts a Windows Forms application, it will include this similar basic code.

     Module StartsHere        Public Sub Main(  )           Dim startForm As New Form1           Application.Run(startForm)        End Sub     End Module 

New in 2005. Visual Basic 2005 includes a new Windows application framework model, a structure that provides events and actions during the startup, running, and shutdown of your application. This feature is enabled or disabled through the Application tab of the Project Properties panel. When this model is enabled, the default Main routine resides in a separate special class associated with the application instead of in the default form's class. When creating new Windows Forms applications in Visual Basic 2005, this framework is enabled by default.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net