Section 2.1. Getting a VB Program to Run

   

2.1 Getting a VB Program to Run

Any Visual Basic executable ” i.e., a Windows Forms or Windows console application ” has a single application-level entry point, a subroutine named Main . Main must be a method of the executed class.

The web applications (either ASP.NET applications or web service applications) that you develop with Visual Studio are not executables. They exist as dynamic link libraries (DLLs) in the system's disk storage. ASP.NET applications may also rely on just-in-time compilation and be resident solely in memory.

Main must not only exist, it must also be:

A public routine

In VB 6, Main could be either public or private. In VB.NET, it must be public to be visible as an entry point.

A static or shared routine

Its declaration must include the Shared keyword. A single Main method must be shared by all application instances; it cannot be an instance method. Thus, all methods called by Main must also be static (or shared) methods ; a shared method is unable to invoke an instance method.

This section focuses on executable programs. These programs exclude code libraries, as well as ASP.NET applications and web service applications, all of which are compiled as dynamic link libraries.

2.1.1 Console Applications

The requirement that there must be a subroutine named Main capable of serving as the executable's entry point is clear in a console application like the one shown in Example 2-1. The routine creates a module named modMain; that module in turn contains a subroutine named Main , which is the sole executable routine in the application. At runtime, Main serves as the program entry point; the Common Language Runtime finds the Main procedure, displays a message to the console, and then terminates the program.

Example 2-1. A simple console application
 Option Strict On Imports Microsoft.VisualBasic Imports System Public Module modMain Public Sub Main    Console.WriteLine("This is a console application.") End Sub End Module 

The code in Example 2-1 should be familiar to Visual Basic programmers, since it depicts one of the methods used to define a program entry point in VB 6. In VB 6, this program would be stored in a separate standard module ( .bas ) file, which is shown in Example 2-2. As long as Main is identified as the startup point for the Visual Basic project, the VB runtime would find Main and execute it.

Example 2-2. A VB 6 version of a simple console application
 Option Explicit Private Sub Main(  )    MsgBox "A simple console application." End Sub 

Although the VB.NET program in Example 2-1 seems similar to the VB 6 program in Example 2-2, under the hood, we would find important differences. If we use ILDASM to graphically depict the members of the VB.NET console application, as Figure 2-1 shows, we see that the VB.NET compiler translates our code module into a public class and gives it a single method, Main . If we examine the intermediate language (or IL) for Main (see Figure 2-2), we see that it is marked as the program entry point and that it is a shared method, rather than an instance method. The VB.NET compiler and the .NET Common Language Runtime, it would seem, have transformed our simple code module into a self-executing class.

Figure 2-1. The modMain module in ILDASM
figs/vnl2_0201.gif
Figure 2-2. IL for the Main procedure
figs/vnl2_0202.gif

2.1.2 Windows Forms Applications

The notion of a self-executing class is novel . [1] However, if we use Visual Studio .NET to create the simple Windows Forms application shown in Example 2-3, it is unclear exactly how the application is able to start, since the only entry point appears to be New , the class constructor. Because New executes when the New keyword is encountered (and as a result, the class is instantiated ), it clearly cannot serve as a program entry point.

[1] If you designate a form as an application's startup object, previous versions of Visual Basic appear to create self-executing forms. This appearance applies only to forms, not to other Visual Basic classes ( .cls files). In fact, it's not really true of forms; Visual Basic supplies the startup code, which includes the code used to instantiate the startup form. The program entry point is not located in the form.

Example 2-3. A simple Windows Forms application
 Public Class Form1     Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code "     Public Sub New(  )         MyBase.New(  )         'This call is required by the Windows Form Designer.         InitializeComponent(  )         'Add any initialization after the InitializeComponent(  ) call     End Sub     'Form overrides dispose to clean up the component list.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)         If disposing Then             If Not (components Is Nothing) Then                 components.Dispose(  )             End If         End If         MyBase.Dispose(disposing)     End Sub     'Required by the Windows Form Designer     Private components As System.ComponentModel.IContainer     'NOTE: The following procedure is required by the Windows Form Designer     'It can be modified using the Windows Form Designer.      'Do not modify it using the code editor.    Friend WithEvents Button1 As System.Windows.Forms.Button    <System.Diagnostics.DebuggerStepThrough(  )> _    Private Sub InitializeComponent(  )       Me.Button1 = New System.Windows.Forms.Button(  )       Me.SuspendLayout(  )       '       'Button1       '       Me.Button1.Location = New System.Drawing.Point(104, 48)       Me.Button1.Name = "Button1"       Me.Button1.Size = New System.Drawing.Size(88, 48)       Me.Button1.TabIndex = 0       Me.Button1.Text = "Button1"       '       'Form1       '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)       Me.ClientSize = New System.Drawing.Size(292, 165)       Me.Controls.AddRange(New System.Windows.Forms.Control(  ) {Me.Button1})       Me.Name = "Form1"       Me.Text = "Form1"       Me.ResumeLayout(False)    End Sub #End Region    Private Sub Button1_Click(ByVal sender As System.Object, _                              ByVal e As System.EventArgs) _                              Handles Button1.Click       MsgBox("This is a Windows Forms application.")    End Sub End Class 

However, ILDASM gives a slightly different picture of this Windows Forms application. In Figure 2-3, we see that in addition to the methods defined in the source code either by us or in the code autogenerated by Visual Studio, the VB.NET compiler has generated a Main method automatically and transparently .

When examining the IL for the Main method (see Example 2-4), it becomes clear why code for the Main method is not more obvious and how the method itself works. As Example 2-4 shows, the method is declared public but is marked as hidden. Once again, the method is declared static or shared. The method operates by invoking the class constructor, then calling the Application object's Run method to launch an instance of the form. Note that the Application object's Run method is a shared or static method, rather than an instance method.

Figure 2-3. The Windows Forms application in ILDASM
figs/vnl2_0203.gif
Example 2-4. IL for the Main method
 .method public hidebysig static void  Main(  ) cil managed {   .entrypoint   .custom instance void             [mscorlib]System.STAThreadAttribute::.ctor(  ) = ( 01 00 00 00 )    // Code size       14 (0xe)   .maxstack  8   IL_0000:  nop   IL_0001:  newobj instance void WinApp1.Form1::.ctor(  )   IL_0006:  call   void             [System.Windows.Forms]System.Windows.Forms.Application::Run(            class [System.Windows.Forms]System.Windows.Forms.Form)   IL_000b:  nop   IL_000c:  nop   IL_000d:  ret } // end of method Form1::Main 

We can simplify our Windows Forms application by coding outside of Visual Studio. The result is shown in Example 2-5.

Example 2-5. A simple Windows forms application created without Visual Studio
 Option Strict On Imports Microsoft.VisualBasic Imports System Imports System.ComponentModel Imports System.Windows.Forms Public Class MyForm    Inherits Form    Public Shared Sub Main(  )        Application.Run(New MyForm)    End Sub    Public Sub New(  )       MyBase.New(  )    End Sub    Public Sub Form_Load(sender As Object, e As EventArgs) _               Handles MyBase.Load       MsgBox("The Windows Forms application.")    End Sub End Class 
   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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