|< Day Day Up >|
Programs written in Visual Basic .NET are designed to run on the .NET Framework (see Figure 2-1). Although a programmer does not need to understand everything about the Framework to be able to write programs for it, there are a few concepts that a programmer should understand because they affect the language and the behavior of some parts of the language.
Figure 2-1. The .NET Framework
As discussed briefly in the previous chapter, the output of a Visual Basic .NET compiler is an assembly that contains all of the program's code and metadata. However, this assembly is not a Windows executable in and of itself, because it contains IL instead of x 86 instructions that an Intel-compatible microprocessor can understand. For an assembly to be run, it must be loaded within the .NET Framework runtime environment and compiled from IL into actual x 86 instructions that can be run under Windows. This translation step, known as Just-In-Time (or JIT) compiling, allows the Framework to inspect the IL code for security and correctness before it is actually run.
The Framework also provides a number of services to assemblies, such as automatic memory management, serialization, security, and interoperability with COM and Win32 APIs. This means that programmers do not have to write code to implement these services themselves .
Program Startup and Termination
The fact that the subroutine in the HelloWorld example in the previous chapter was named Main is not coincidental. After the Framework has loaded an assembly and translated the IL into x 86 instructions, it looks for a method named Main and starts executing the program with that method. The Main method is also known as the entry point of the program.
The entry point method can take several forms. In the simplest form, as in the HelloWorld example, it is just a subroutine that is called by the Framework and returns when the program is finished.
Module CommandLine Sub Main() ... End Sub End Module
Main can also be declared as a function that returns an Integer value. In that case, the value returned by Main will be passed out to the calling environment ”for a console application, this sets the error level of the program when it is done. The method can also be declared to take a one-dimensional array of String as a parameter; at runtime, this array will be filled in by the Framework with the arguments, if any, passed in on the command line. The following example shows an application that prints the command-line arguments passed to the program and returns a value of 1.
Module CommandLine Function Main(ByVal Args() As String) As Integer For Each Arg As String In Args Console.WriteLine(Arg) Next Arg Return 1 End Function End Module
Entry points are only called for applications. Class libraries (DLLs) don't have explicit entry points, because they are loaded by applications and are never executed on their own.
|< Day Day Up >|