2.1 Getting a VB Program to RunAny 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.
Main must not only exist, it must also be:
2.1.1 Console ApplicationsThe 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 applicationOption 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 applicationOption 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 ILDASMFigure 2-2. IL for the Main procedure2.1.2 Windows Forms ApplicationsThe 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.
Example 2-3. A simple Windows Forms applicationPublic 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 ILDASMExample 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 StudioOption 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 |